LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Create file using tar and ls commands (https://www.linuxquestions.org/questions/linux-newbie-8/create-file-using-tar-and-ls-commands-705046/)

Ejdaha 02-16-2009 08:10 AM

Create file using tar and ls commands
 
Hi All
I have a file aa.txt

I want to tar it. I'm using
tar -cvf aa.tar aa.txt

There's only one .txt file in my folder. I want to ls my folder, get the file's name and create a tar with its name

ls *.txt
aa.txt

And I want not to give the tar file's name directly, but use the result of ls as tar file's name. It's name will be aa.txt.tar

Thank you very much

i92guboj 02-16-2009 08:42 AM

You don't need ls, you probably want to look at the --wildcards parameter for tar.

jschiwal 02-16-2009 08:55 AM

Suppose that you wanted to archive all pdf files in ~/Documents, but many are in subdirectories. Or perhaps you want to archive pdf files in ~/Documents/ that are over 30 days old.

cd ~
find Documents -type f -iname "*.pdf" -ctime +30 -print0 | xargs -0 tar -C ~/ -czf pdf_archive_Feb16_2009.tar.gz

I used -print0 in case the filenames contained white space. Null characters are used instead of \n between filenames. The -0 argument to xargs will use those nulls to separate arguments.

You can do something similar with ls:
ls Documents/*.pdf | tr '\n' '\0' | xargs -0 tar -C ~/ -czf pdf_archive_Feb16_2009.tar.gz


All times are GMT -5. The time now is 02:06 PM.