If I have an archive, for example, some.zip that contains one or more files, how can I extract only one file (I know the name of the file) with 7-Zip from the command line in Windows?
- 12,090
- 23
- 70
- 90
- 683
- 1
- 5
- 4
5 Answers
As a follow-up to surfasb's answer, add a -r flag at the end to recurse:
7z e [archive.zip] -o[outputdir] [fileFilter] -r
Multiple filters support:
7z e [archive.zip] -o[outputdir] [fileFilter_1] [fileFilter_2] -r
Example:
Multiple filters command line:
7z e archive.zip -o outputdir *.xml *.dll -r
PS: I use 7za.exe instead of 7z.exe. This is the actual command I use in my script:
7za.exe x archive.zip -o outputdir *.xml *.pdb *.exe *.ocx *.dll -r
-
2How can I add multiple file filters, say `.XML` and `.zip`? Sorry if I'm hijacking this thread, I just didn't want to add a duplicate question. – Fr0zenFyr Jun 21 '14 at 05:25
-
2Should be fine by separating the filters by space. See the edited answer above. :) – zionyx Aug 26 '14 at 14:06
-
And how do I extract a specific file from an archive inside the archive? Lets say the file I'm looking for is "MyFile.txt" inside "SubArchive.zip" inside "MainArchive.zip". Is this possible? – PeterCo Oct 27 '17 at 12:14
-
1@PeterCo, I think the command is only capable to extract `SubArchive.zip` from the `MainArchive.zip` in your case. You may run a follow up command to extract `MyFile.txt` from `SubArchive.zip` after the initial extraction. – zionyx Jan 05 '18 at 09:09
-
You also want to double-quote those wildcard filters, e.g. "*.exe" or "*.pdf". Without that, the filter will skip filenames with embedded space characters. – Dave Hein May 22 '21 at 12:31
-
Useful: use `7z x` instead of `7z e` to keep the directory structure when extracting. – Basj Jul 22 '21 at 18:59
You just add the filename at the end.
7z e [archive.zip] -o [outputdir] [fileFilter]
- 103
- 3
- 22,452
- 5
- 52
- 77
-
1How can I add multiple file filters, say `.XML` and `.zip`? Sorry if I'm hijacking this thread, I just didn't want to add a duplicate question. – Fr0zenFyr Jun 21 '14 at 05:25
-
It's a different question @Fr0zenFyr. To make it clear that it is not duplicate, you can reference this question in your own, and then specify how yours is different. – music2myear Jan 23 '17 at 23:18
-
@music2myear: Thanks for clarification. Top voted answer by zionyx already includes a solution. My comment is over 2 years old, so it would be a reasonable guess to assume that I either used above solution or already posted a question and found an answer (BTW, this was my case). My [resolved post is on SO](http://stackoverflow.com/q/24339419/1369473) and original thread created before my comment is [here](http://superuser.com/q/770255/243637) so I couldn't link to this question. :) – Fr0zenFyr Jan 25 '17 at 10:00
-
1Note : as it is written here, do not put space between -o and outputdir. – kingsjester Oct 25 '19 at 09:38
If you look at the man page for 7z you will find that the following command can be used to extract a file from a 7z archive (though the usage of path is missing from the man page):
7z x <archive> <path to file>
Examples:
7z x backup.7z *.html
7z x backup.7z folderwithin/myfile.html
Alternatively you could use e.
The command line version users guide seems to have more information on the actual usage.
- 9,000
- 1
- 19
- 34
- 305
- 3
- 3
Note that 7z has the following syntax (observe the spaces and quotes surrounding the "-oMy Folder" option to set the output folder name, took me hours to figure out, as I originally did this – the wrong way: * -o "My Folder" *):
7z e "my zip.zip" "-oMy Folder" *.jpg "all of these.*" -r
- 41
- 1
I found that on zsh command line, with 7-zip 16.06, that I had to put double-quotes around the wildcard filter argument. For example, this did not find any PDF files to extract:
7z e "archive has pdf in subdirectory.zip" -r *.pdf
but quoting the wildcard filter did find and extract the PDF file that was in a subdirectory of the zip archive, like this:
7z e "archive has pdf in subdirectory.zip" -r "*.pdf"
- 111
- 2