116

How does one extract a specific folder from a zipped archive to a given directory?

I tried using

unzip "/path/to/archive.zip" "in/archive/folder/" -d "/path/to/unzip/to"

but that only creates the folder on the path I want it to unzip to and does nothing else.

7 Answers7

134
unzip /path/to/archive.zip "in/archive/folder/*" -d "/path/to/unzip/to"
sue
  • 1,356
  • If you get 'caution: filename not matched: '/dir1/dir2/' when you try to extract something like '/dir1/dir2/' - remove first '/' from the path, so it becomes 'dir1/dir2/*'. Double-check the existence of the path if this won't work. – mktplus Jul 24 '21 at 10:23
47

Try:

unzip /path/to/archive.zip 'in/archive/folder/*' -d /path/to/unzip/to
Mark Adler
  • 1,995
  • 6
    did this? I get caution: filename not matched: foldername/\* when I run unzip $repozip "$2-master/\*" -d /srv/www/magento/ where $2 is the folder name in the zip I want to pull all the files and folders out of – Quantum Oct 18 '13 at 02:43
  • 1
    Then the filename was not matched. Do unzip -l xxx.zip to see what's in xxx.zip. – Mark Adler Oct 18 '13 at 05:24
  • 1
    I just tried this and it didn't work for me either! – slm Nov 07 '13 at 20:57
  • 10
    Worked without , just "path/*". – Alex Oct 28 '14 at 21:24
  • 18
    Is there a way to extract contents of "in/archive/folder/*" that does not preserve path "in/archive/folder/"? I end up using mv afterwards to get files where I needed them. – jerrygarciuh Nov 12 '14 at 20:29
  • 9
    Use -j for that. – Mark Adler Nov 12 '14 at 20:55
  • 15
    -j strips all path info, and all files go into the target folder. Is there a way to remove only the common part of the path? Akin to zip -r. – Bob Stein Feb 10 '15 at 16:35
  • 1
    I don't know what you mean by "akin to zip -r". If you're asking for a way to strip some of the path elements, but not all of the path elements in the names stored in the zip file, then no, unzip does not have a means to do that. – Mark Adler Mar 01 '15 at 16:02
  • 1
    the double quotation for the in/archive/folder didn't work for me. @sue's single quotation solution worked find for me – georg Jan 13 '16 at 11:00
  • 2
    @BobStein-VisiBone I need to do this to fill an existing directory structure with some files from the ZIP file. What I've done is just unzip naively, giving me the directory structure I don't want, but then do rsync -a path/i/dont/want/dir/ path/i/do/want/dir, then delete the unwanted directory. Note that the trailing slash on the first and the lack of trailing slash on the second are important for rsync. – Mike Jan 18 '17 at 17:37
8

The existing two answers are both correct, but it's a bit tricky to specify the target directory, that should be better clarified.

Let's say /target/root/ is the target dir upon the original unzip action, e.g.:

unzip -qq src.zip -d "/target/root/"

Then, we need to use the same /target/root/ as the target dir afterward even though we want to extract only a specific sub-directory, as the way unzip works:

unzip -qq src.zip "sub/dir/*" "/target/root/"

After all, the rule is actually simple, use the same target root directory for the -d option.

BTW, the -qq option is for unzip to be really quiet, feel free to remove it.

ryenus
  • 195
  • FYI: I did unzip -qq 2.3.zip "magento2-2.3/vendor/*" "vendor/" and got caution: filename not matched: vendor/`. This is on an ubuntu install in bash. I rarely unzip, probably did something else wrong... – Krista K Jun 20 '19 at 17:41
  • For me this unzipped the files in that specific directory, but did not pick up subdirectories; is that a different command? – codeMonkey Nov 26 '19 at 19:14
  • The first time I read this I couldn’t make any sense of it at all.  Then I read it a second time and I thought I understood it.  Then I read it a third time and got a different meaning.  Your answer should be [better] clarified. … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … Also, you refer to “the original unzip action”.  What makes you think that there was a previous unzip action?  What if I want to look at data for April 2017, so I download 2017.zip from the server and I want to extract the April folder (and nothing else)? – G-Man Says 'Reinstate Monica' Jul 15 '22 at 16:25
4

To extract subfolders as well within the desired one use **/* wildcard:

unzip src.zip 'path/in/archive/**/*' -d /target/dir/

It is important to put the pattern into single quotes, to prevent the shell from interpreting and expand the wildcard characters.

Tested on UnZip 6.00 of 20 April 2009, by Info-ZIP.

Z4-
  • 157
  • for the people who don't want to think, just get it done, this is the answer – Jake Jan 19 '22 at 18:32
  • 1
    -1: Misinfo. 1. No, you don't need folder/**/* to "extract subfolders within the desired folder". folder/* already extracts the whole folder and all subfiles and all subfolders and all files within all subfolders. The whole tree is extracted. 2. You don't need single quotes. The shell never expands asterisks inside any quotes at all, whether they are double or single. 3. I am using the exact same version of UnZip. So what I just said is the same for you too. – Mitch McMabers May 05 '22 at 18:42
  • @Mitch McMabers Please have a look at my answer to know why this is not misinformation - https://unix.stackexchange.com/a/748349/356166 – Hrishikesh Kadam Jun 08 '23 at 16:20
1

I had to specify both * and **/* for every path/in/archive/

unzip src.zip 'path/*' 'path/**/*' -d /target/dir/

this works.

Omitting either one, skips any files present.

Example:

unzip aar.aar 'res/**/*' 'res/*' -d /lib/res/
  • This worked as expected, it unzips files AND directories stored in the specified folder inside the zip file. – ZacWolf Oct 05 '22 at 17:35
1

To continue on @Z4- and @scriptmaster answer.

I tested unzip on all three OS -

  • Linux (Ubuntu 23.04)
  • macOS (Ventura 13.4)
  • Windows 11 (Git BASH 2.41.0.windows.1)

All the OS had the following version -

unzip --help
UnZip 6.00 of 20 April 2009, by Info-ZIP.  Maintained by C. Spieler.

On Ubuntu and macOS, the following command was sufficient to extract a specific folder with all its contents including subfolders and its contents.

unzip src.zip 'path/*' -d /target/dir/

But on Git for Windows, the following command is required.

unzip src.zip 'path/*' 'path/**/*' -d /target/dir/

So the best way would be to extract all files to some temp directory and then move to the target directory

unzip src.zip -d /temp/dir/
mv /temp/dir/path /target/dir/
0

I'm new in MacOS (arm) but the following works perfectly:

unzip <*.zip> -d <my_folder>
27P
  • 101