?

Log in

No account? Create an account

GEEK: How to tar up a list of filenames - Open Knowledge — LiveJournal

Mar. 15th, 2007

09:40 pm - GEEK: How to tar up a list of filenames

Previous Entry Share Next Entry



Suppose you have a file named "files.txt" that contains the following list of filenames:

/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/CarbonCore
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/Metadata.framework/Versions/A/Metadata
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/OSServices.framework/Versions/A/OSServices
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/SearchKit.framework/Versions/A/SearchKit
/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation

If you want to tar up the actual files you can do so with this command:

cat files.txt | xargs tar cvzf files.tgz

cat - displays the contents of files.txt
| - pipes the output of cat to xargs
xargs - reads the filenames passed to it by cat, and executes tar with them as arguments
c - create tar file
v - verbose
z - gzip the resulting tar file
f - write to file
files.tgz - the name of the file

Comments:

[User Picture]
From:polyanarch
Date:March 16th, 2007 04:52 am (UTC)

I'll stick with windoze

(Link)
Install.exe

Done
(Reply) (Thread)
[User Picture]
From:ernunnos
Date:March 16th, 2007 05:46 am (UTC)

Re: I'll stick with windoze

(Link)
xargs is your friend. Just for completeness sake, you might want to replace that

cat files.txt

with

sed -e 's/ /\\ /g' files.txt. xargs also takes spaces as file delimiters.
(Reply) (Thread)
[User Picture]
From:crasch
Date:March 16th, 2007 07:33 am (UTC)

Re: I'll stick with windoze

(Link)
Thanks!
(Reply) (Parent) (Thread)
[User Picture]
From:peamasii
Date:March 16th, 2007 08:01 am (UTC)
(Link)
What? You don't need a pipe to tar a list of files. I only use xargs if I'm going to pipe from 'find' or some other useful command.

tar czf --files-from=files.txt files.tgz

Remember to be as lazy as possible when writing scripts.
(Reply) (Thread)
[User Picture]
From:crasch
Date:March 16th, 2007 03:57 pm (UTC)
(Link)
Thanks! I didn't know how lazy I could be--I didn't know about the --files-from flag.
(Reply) (Parent) (Thread)
[User Picture]
From:deftly
Date:March 16th, 2007 04:47 pm (UTC)
(Link)
It's only in gnu tar (also the -T option). Older versions of tar have a -I include_file and -X exclude_file.
(Reply) (Parent) (Thread)
[User Picture]
From:crasch
Date:March 18th, 2007 09:47 am (UTC)
(Link)
Ah yes. Unfortunately, it doesn't seem to be an option on Mac OS X's version of tar.
(Reply) (Parent) (Thread)
[User Picture]
From:jbsupergenius
Date:March 17th, 2007 03:57 am (UTC)

Behold the power of the back tick

(Link)
Lets not forget:

tar cvzf files.tgz `cat files.txt`

(Reply) (Thread)
[User Picture]
From:crasch
Date:March 18th, 2007 09:46 am (UTC)

Re: Behold the power of the back tick

(Link)
Thanks!
(Reply) (Parent) (Thread)