LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 02-16-2009, 08:10 AM   #1
Ejdaha
Member
 
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
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
 
Old 02-16-2009, 08:42 AM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

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

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


Reply



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 10:15 AM
how to create execute file for linux telnet commands siva karthik Programming 7 03-10-2006 12:50 AM
create an RPM install file from tar? Siljrath Linux - Newbie 8 01-07-2005 01:33 PM
how to create a tar.gz file? fuelinjection Linux - General 2 07-09-2004 05:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:55 AM.

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