Extract only a specific file from a zipped archive to a given directory

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.


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 (obviously, it only works for regular files, not symlinks, devices, directories...). 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

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

Tags:

Zip