171

I need to extract a single file from a ZIP file which I know the path to. Is there a command like the following:

unzip -d . myarchive.zip path/to/zipped/file.txt

Unfortunately, the above command extracts and recreates the entire path to the file at ./path/to/zipped/file.txt. Is there a way for me to simply pull the file out into a specified directory?

146

You can extract just the text to standard output with the -p option:

unzip -p myarchive.zip path/to/zipped/file.txt >file.txt

This won't extract the metadata (date, permissions, …), only the file contents. That's the price to pay for the convenience of not having to move the file afterwards.

Alternatively, mount the archive as a directory and just copy the file. With AVFS:

mountavfs
cp -p ~/.avfs"$PWD/myarchive.zip#"/path/to/zipped/file.txt .

Or with fuse-zip:

mkdir myarchive.d
fuse-zip myarchive.zip myarchive.d
cp -p myarchive.d/path/to/zipped/file.txt .
fusermount -u myarchive.d; rmdir myarchive.d
| improve this answer | |
  • Will it work for binary files, say a jar file? – Naftuli Kay Jun 1 '11 at 1:01
  • 1
    @TKKocheran: Jar files are zips, so the unzip and fuse-zip methods will obviously work. The AVFS method also works, because AVFS guesses the format based on file names and knows about .jar; if your file is named differently you might need to tell AVFS to use its zip handler, e.g. ~/.avfs$PWD/foo.apk#uzip/META-INF. – Gilles 'SO- stop being evil' Jun 1 '11 at 7:19
  • 1
    actually, I meant extracting a JAR/binary file from a ZIP archive. I haven't had the chance to test it out yet, can you see any issues using the first command above to extract binary files? – Naftuli Kay Jun 1 '11 at 18:41
  • 2
    @TKKocheran: There's no problem, -p extracts the file as-is (-c does text conversion). – Gilles 'SO- stop being evil' Jun 1 '11 at 20:50
  • 2
    I think the answer of @Myles is more elegant, because it doesn't require output redirection and it preserves file attributes. – gertvdijk Dec 4 '12 at 15:42
169
unzip -j "myarchive.zip" "in/archive/file.txt" -d "/path/to/unzip/to"

Enter full path for zipped file, not just the filename. Be sure to keep the structure as seen from within the zip file.

This will extract the single file file.txt in myarchive.zip to /path/to/unzip/to/file.txt.

| improve this answer | |
  • 1
    It doesn't handle the case where you want the generated filename to be different. – Oleg Vaskevich Oct 27 '14 at 21:21
  • 7
    the "-j" paramter: junk paths. The archive's directory structure is not recreated; all files are deposited in the extraction directory (by default, the current one). the "-d" parameter: extract files into exdir – e1i45 Jan 29 '15 at 16:14
  • 1
    @OlegVaskevich, the question doesn't request target filename to be different – Tapemaster May 11 '15 at 10:49
  • 1
    FYI to find out the path of files in zip, use less: superuser.com/questions/216617/… – Yibo Yang Oct 24 '17 at 6:17
  • Some unzip binaries don't have the -d flag. I won't work in every linux – neves Dec 6 '19 at 15:02
27

Simpler version:

unzip ARCHIVE_NAME PATH_OF_FILE_INSIDE_ARCHIVE

This will recreate PATH_OF_FILE_INSIDE_ARCHIVE in current directory but only extracts specified file.

To list all files in a Zip archive:

unzip -l ARCHIVE_NAME
| improve this answer | |
  • This works with wildcards too, at least in CentOS - e.g. unzip foo.zip *.bar – aland May 21 at 14:32
5

On macOS, which by default uses Info-Zip

First list off the files to find what you want

unzip -l my.zip

Then provide a list of files to extract from the archive

unzip my.zip annoying/path/to/file

Combine with -p for stdout

unzip -p my.zip annoying/path/to/file >./file
| improve this answer | |
  • 2
    I don't understand why people downvote without any obvious reason. This is a correct answer (not just for macOS) – FractalSpace Sep 6 '18 at 20:09
  • @FractalSpace It patently isn’t, it does the opposite of what OP wants. – Konrad Rudolph Jan 14 '19 at 16:36
  • The key is getting an individual file. Output to stdout and the redirect using the -p flag is trivial, but I've added it explicitly – Cameron Lowell Palmer Jan 15 '19 at 15:27
3

simple use:

unzip zipfile.zip path/inside/zip/file.txt

and it will inflate the file.

$ unzip -l ./../html.zip | grep wp-config

     3328  07-22-2019 15:10   html/wp-config.php

     2898  01-07-2019 23:30   html/wp-config-sample.php

$ unzip ./../html.zip html/wp-config.php

     Archive:  ./../html.zip
     inflating: html/wp-config.php

$ ls -lrth

     total 4.0K
     drwxr-sr-x 2 apache apache 4.0K Jul 26 14:41 html

$ ls -lrth html/*

     total 4.0K
     -rw-rw-rw- 1 apache apache 3.3K Jul 22 15:10 wp-config.php
| improve this answer | |
  • 1
    Can also add the -d outpath argument to specify where the file is put in. – Efren Sep 18 '19 at 2:45
  • 1
    And the -j argument to avoid the compressed paths – Efren Sep 18 '19 at 2:56
0

Extract to a relative dir

unzip -j -d relativedir archive.zip path/in/archive/file.ext

Extract to the current dir

unzip -j -d . archive.zip path/in/archive/file.ext

Extract to absolute dir

unzip -j -d /absolutedir archive.zip path/in/archive/file.ext
| improve this answer | |

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.