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?
5 Answers
You just add the filename at the end.
7z e [archive.zip] -o [outputdir] [fileFilter]
-
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. Commented Jun 21, 2014 at 5: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. Commented Jan 23, 2017 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 and original thread created before my comment is here so I couldn't link to this question. :) Commented Jan 25, 2017 at 10:00
-
1Note : as it is written here, do not put space between -o and outputdir. Commented Oct 25, 2019 at 9:38
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. Commented Jun 21, 2014 at 5:25 -
2Should be fine by separating the filters by space. See the edited answer above. :)– zionyxCommented Aug 26, 2014 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?– PeterCoCommented Oct 27, 2017 at 12:14
-
1@PeterCo, I think the command is only capable to extract
SubArchive.zip
from theMainArchive.zip
in your case. You may run a follow up command to extractMyFile.txt
fromSubArchive.zip
after the initial extraction.– zionyxCommented Jan 5, 2018 at 9: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. Commented May 22, 2021 at 12:31
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.
-
2
-
Yes, the "folderwithin" is a folder at the root of the archive. Commented Oct 25, 2019 at 8:37
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
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"