Suppose I have a large zip file (>50GB) and I want to extract some files off of it from the command line.

To get the files I run the command:

unzip -l myfile.zip | grep "foo"

which gives me a list of zip entries; how do I extract those files that pass through the grep filter? I tried using xargs unzip -j but I'd like some cleaner solution as the zip entries require cleaning of useless information.

  • 1
    You can just do unzip -j myfile.zip '*foo*' – Stéphane Chazelas Jul 28 '17 at 15:20
  • Nice and clean. – Warrior Jul 28 '17 at 15:32
  • be careful if you're grepping for anything that unzip -l also reports in the header or trailer; e.g. "archive", "zip", "length", "date", "time", "name", "file", or numbers that match the total bytes or number of files. – Jeff Schaller Jul 28 '17 at 16:32
  • If any of the existing answer solved your problem, please accept it by clicking the checkmark next to it. Thank you! – Jeff Schaller Nov 26 '17 at 15:37

Stéphane has the right idea to pass zip the wildcard corresponding to the filenames that you'd like to extract. Parsing the output of unzip means you have to watch out for the header and trailer lines that come along.

Use something like:

unzip -j myfile.zip '*foo*'

being careful to quote the wildcards from the shell.

If you continue along the direction of grepping unzip's output, strip out the header and trailer and reduce it to the filename column:

unzip -l myfile.zip | sed '1,3d; /---------                     -------/d; $d'|cut -c31-
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.