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 Answers 7

134
unzip /path/to/archive.zip "in/archive/folder/*" -d "/path/to/unzip/to"
1
  • 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. Jul 24, 2021 at 10:23
47

Try:

unzip /path/to/archive.zip 'in/archive/folder/*' -d /path/to/unzip/to
10
  • 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, 2013 at 2:43
  • 10
    Worked without \, just "path/*".
    – Alex
    Oct 28, 2014 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. Nov 12, 2014 at 20:29
  • 9
    Use -j for that.
    – Mark Adler
    Nov 12, 2014 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, 2015 at 16:35
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.

3
  • FYI: I did <kbd>unzip -qq 2.3.zip "magento2-2.3/vendor/*" "vendor/"</kbd> and got <kbd>caution: filename not matched: vendor/`</kbd>. This is on an ubuntu install in bash. I rarely unzip, probably did something else wrong...
    – Krista K
    Jun 20, 2019 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, 2019 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)? Jul 15, 2022 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.

3
  • for the people who don't want to think, just get it done, this is the answer
    – Jake
    Jan 19, 2022 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. May 5, 2022 at 18:42
  • @Mitch McMabers Please have a look at my answer to know why this is not misinformation - unix.stackexchange.com/a/748349/356166 Jun 8, 2023 at 16:20
2

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/
1
  • This worked as expected, it unzips files AND directories stored in the specified folder inside the zip file.
    – ZacWolf
    Oct 5, 2022 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/
-1

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

unzip <*.zip> -d <my_folder>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .