Zeta
Zeta

Reputation: 534

Unzip list of files that are specified in txt files

I have a txt file that contains a list of file names. In bash, how do I unzip only those files specified in the list from a zip file?

Upvotes: 2

Views: 1501

Answers (2)

Barton Chittenden
Barton Chittenden

Reputation: 4416

If you're working with a list of files at the command line, xargs is almost always the best answer -- it handles file names with spaces cleanly, and it gets around the limit of the number of arguments. I would suggest this:

tr '\n' '\0' <filelist.txt | xargs -0 unzip -q /path/to/zipfile

The use of tr '\n' '\0' <filelist.txt takes your list of files and substitutes nul characters for new lines. xargs -0 reads a nul delimited list of files and tacks that onto the agument list of the following command.

This will break if your list of filenames uses '\r\n' or '\r' style end of lines.

Upvotes: 2

fge
fge

Reputation: 121710

This should work:

unzip -q /path/to/zipfile $(cat thetxtfile)

Of course, this command needs to be issued in a directory which is preferably empty to begin with.

Note that if there are spaces into your file names, this won't work and you need to do this instead:

while read thefile; do unzip -q /path/to/zipfile "$thefile"; done <thetxtfile

Upvotes: 4

Related Questions