I have a .jar file which can be opened with the Command line tool "unzip", which has this option called -p so you can grab certain files. But how do you do this with a folder? Here's what this jar file is structured like.

|   |-Content
|        |-My Files
|   |-First Folder
|        |-Second Folder
|             |-Files

The Folder "folder" that I want to grab also has a lot more files just than "My Files" and "Files".

The Command I tried: unzip dir/to\ a\ folder/My.jar -d dir/to\ a/desired\ folder -p folder/ | image.png

Without the other file: unzip dir/to\ a\ folder/My.jar -d dir/to\ a/desired\ folder -p folder/

I tried this with the jar command, and that seemed to work, but I specifically want to do this with unzip.

And the error I got is:

caution: filename not matched: -p
caution: filename not matched: assets/,

Unzip Help Menu I have: Unzip on macOS 10.13.6

  • Which unzip command are you using? The Info-Zip one seems to be the standard on Linux distributions. That does have a "-p" option but it doesn't do what you need. Can you provide more details about your unzip please? Try running unzip -h. – pmqs Apr 21 '20 at 21:02
  • I attached an image of the one that came with my mac, unzip -h isn't an option. – SomePerson Apr 21 '20 at 22:23

Good, that's just standard info-zip unzip you are running.

The -p option you are using doesn't do what you expect. Below is the man page for entry for -p.

   -p     extract files to pipe (stdout).  Nothing but the  file  data  is
          sent  to  stdout,  and  the files are always extracted in binary
          format, just as they are stored (no conversions).

Let's work though an example. First create a directory tree that is similar to yours

`-- folder
    `-- Content
        |-- FirstFolder
        |   `-- SecondFolder
        |       `-- Files
        |           |-- f1
        |           |-- f2
        |           `-- f3
        `-- MyFiles
            |-- a1
            |-- a2
            `-- a3

Stick that into a zip

$ zip -r try.zip data
$ unzip -l data.zip
Archive:  try.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  04-22-2020 08:59   data/
        0  04-22-2020 08:59   data/file1
        0  04-22-2020 08:59   data/file2
        0  04-22-2020 08:56   data/folder/
        0  04-22-2020 08:57   data/folder/Content/
        0  04-22-2020 08:57   data/folder/Content/FirstFolder/
        0  04-22-2020 08:57   data/folder/Content/FirstFolder/SecondFolder/
        0  04-22-2020 08:57   data/folder/Content/FirstFolder/SecondFolder/Files/
        0  04-22-2020 08:57   data/folder/Content/FirstFolder/SecondFolder/Files/f2
        0  04-22-2020 08:57   data/folder/Content/FirstFolder/SecondFolder/Files/f1
        0  04-22-2020 08:57   data/folder/Content/FirstFolder/SecondFolder/Files/f3
        0  04-22-2020 08:57   data/folder/Content/MyFiles/
        0  04-22-2020 08:57   data/folder/Content/MyFiles/a3
        0  04-22-2020 08:57   data/folder/Content/MyFiles/a1
        0  04-22-2020 08:57   data/folder/Content/MyFiles/a2
        0  04-22-2020 08:59   data/file3
---------                     -------
        0                     16 files

If you want to extract only the folder subtree, you need to give unzip a wildcard that matches the subtree. In this instance data/folder/* will do the trick.

$ unzip -d output try.zip 'data/folder/*' 
Archive:  try.zip
   creating: output/data/folder/
   creating: output/data/folder/Content/
   creating: output/data/folder/Content/FirstFolder/
   creating: output/data/folder/Content/FirstFolder/SecondFolder/
   creating: output/data/folder/Content/FirstFolder/SecondFolder/Files/
 extracting: output/data/folder/Content/FirstFolder/SecondFolder/Files/f2  
 extracting: output/data/folder/Content/FirstFolder/SecondFolder/Files/f1  
 extracting: output/data/folder/Content/FirstFolder/SecondFolder/Files/f3  
   creating: output/data/folder/Content/MyFiles/
 extracting: output/data/folder/Content/MyFiles/a3  
 extracting: output/data/folder/Content/MyFiles/a1  
 extracting: output/data/folder/Content/MyFiles/a2  

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.