1

I have a zip archive that contains a directory hierarchy of files. I want to use the Linux unzip command to extract just the subset of these files that have a particular string somewhere in the directory path leading up to the file.

For example, suppose the file foo.zip contains these files:

a/1.txt
acme/2.txt
a/acme/b/3.txt
a/b/acmenet/c/4.txt
a/b/c/5.txt
a/acme/d/6.php

The Linux command "unzip foo.zip *.txt" will extract all 5 of the "txt" files.

How can this Linux command be modified so that it only extracts the 3 "txt" files that have the string "acme" somewhere in the directory path (so it would extract only 2.txt, 3.txt and 4.txt)?

1

Your example unzip foo.zip *.txt only worked because there was no *.txt file in your working directory. Otherwise *.txt would have been expanded by the shell to the names of the text files in your directory. You need to quote it to be on the safe side.

Since unzip uses globs, use

unzip foo.zip '*acme*/*.txt'

or even

unzip foo.zip '*acme*.txt'

If you don't want to restore the original directory structure and extract the files to the current working directory, add option -j:

unzip -j foo.zip '*acme*/*.txt'

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.