Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 02-16-2009, 09:10 AM   #1
Registered: Jan 2007
Posts: 39

Rep: Reputation: 15
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

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
Old 02-16-2009, 09:42 AM   #2
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 404Reputation: 404Reputation: 404Reputation: 404Reputation: 404
You don't need ls, you probably want to look at the --wildcards parameter for tar.
Old 02-16-2009, 09:55 AM   #3
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 680Reputation: 680Reputation: 680Reputation: 680Reputation: 680Reputation: 680
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to create a tar file from spanning a directory tree? R00ts Linux - General 2 11-01-2006 11:15 AM
how to create execute file for linux telnet commands siva karthik Programming 7 03-10-2006 01:50 AM
create an RPM install file from tar? Siljrath Linux - Newbie 8 01-07-2005 02:33 PM
how to create a tar.gz file? fuelinjection Linux - General 2 07-09-2004 06:22 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:02 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration