Linux unzip one file

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

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?

8 Answers 8

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 . 
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 

@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 .

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?

I think the answer of @Myles is more elegant, because it doesn’t require output redirection and it preserves file attributes.

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 .

Читайте также:  Добавить языковую панель linux

-j : junk paths. The archive’s directory structure is not recreated; all files are deposited in the extraction directory (by default, the current one)

-d : An optional directory to which to extract files.

Multiple files:

unzip -j myarchive.zip in/archive/file.txt another/file.ext -d /path/to/unzip/to 

Entire Sub-directory

unzip -j archive.zip "sub/dir/*" -d "dest/dir" 

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

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:

this is all i needed. very succinct. don’t really see any benefit in using other methods unless you need to extract many files at once.

On macOS, which by default uses Info-Zip

First list off the files to find what you want

Then extract file from the archive

unzip my.zip annoying/path/to/file/in/zip 

Combine with -p for stdout

unzip -p my.zip annoying/path/to/file/in/zip >./file 

or -j for extracting to the current directory (discard junk path)

unzip -j my.zip annoying/path/to/file/in/zip 

with -d you can specify to create an arbitrary directory

unzip -d /path/to/dir my.zip annoying/path/to/file/in/zip 

If you want the file in the -d directory you probably want to combine it with the -j option.

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 

If you want to restore the file’s metadata from the archive, be able to restore files of any type (including symlinks) and with arbitrary names, and also choose the new name of the file, you could use libarchive’s bsdtar (which also supports ZIP format archives) instead of unzip as:

bsdtar -nqvvxpf file.zip -'s/.*/newname/S' some/dir/originalname 

Note: like in unzip , the some/dir/originalname there is taken as a shell wildcard pattern. So if the path of archive member you want to extract does contain * , ? , \ or [ characters, you’ll need to escape them as in:

bsdtar -nqvvxpf file.zip -'s/.*/newname/S' 'som[?]/dir/[*]riginalname' 
bsdtar -nqvvxpf file.zip -'s/.*/newname/S' 'som\?/dir/\*riginalname' 

if the archive member is called som?/dir/*riginalname .

Читайте также:  Класс операционных систем linux

Note that that approach would work with any of the archive formats supported by libarchive, not just zip.

Источник

How to Unzip a ZIP File in Ubuntu / Linux

If you downloaded a file that ends in .tar.gz or .zip, this indicates that it has been compressed.

This guide will walk you through unzipping a zipped file in Ubuntu 18.04 or Ubuntu 20.04.

How to unzip a file in Linux Ubuntu.

  • Access to a terminal window/command line (Ctrl-Alt-T)
  • Zip/unzip utility (included by default)

How to Install the Zip and Unzip Utility in Ubuntu

Ubuntu distributions usually include the zip and unzip utilities. If for some reason yours does not, use the following command to install it:

sudo apt-get install zip unzip

The output in our example confirms that the latest version is already installed:

Install the zip and unzip features command example

Unzip a File Using the Command Line

This guide assumes that you’ve already downloaded a file that has been zipped, and that you know where it’s located. Let’s assume that we have downloaded a file called test.zip to the /home/user/Documents/ directory.

Start by opening the terminal. By default, you should start in the /home/user/ directory.

To list the contents of the directory you are currently viewing enter:

Ubuntu will color-code different entries.

List of file and folders in the Home directory

Directories are colored blue, regular files are colored white (the same as the text you type).

To change to the Documents directory, use the command:

As the folder only contains the test.zip file, the output looks like this:

Content of Documents folder after unzipping

Note: Observe the capitalized D in Downloads. It is necessary to type the name of the directory exactly as you see it on the screen. Linux treats /Downloads and /downloads as two different directories.

Enter ls again. You’ll get a different listing – the content of the Documents folder.

To unzip the test.zip file, enter the following:

The system will decompress the test.zip file, and place a copy of its contents in the /Documents directory.

Note: Normal copying conventions apply. If the test.zip file contains a file named document.txt, and a file with that name already exists in the target directory, the system will ask if you want to overwrite the existing file.

Other Linux Unzip Commands

The zip and unzip commands can be used with additional options to have more control over how they work. Here are just a few common ones.

Читайте также:  Проверка скорости интернета командная строка linux

How to Unzip Multiple ZIP Files

For example the folder /Documents/zipped has several files zipped in it. Use the cd command to change to that directory:

To unzip all the files in that directory.:

The * sign is a wildcard, which means “any number of any characters.” So, any file that ends in .zip would be found and unzipped by entering this command.

How to Test if a ZIP File is Valid

You can use the –t option with the zip command to test the file first. Enter the following:

This is useful if you think the zipped file has been damaged or corrupted.

The screen confirms that all the files are OK and that no errors were reported.

The system will tell you if it detects any errors.

How to Exclude Files When Unzipping a ZIP File

Some zip files have several different files included in them. You can extract all of them, or you can exclude some of them.

To exclude a particular file:

unzip test.zip –x a_particular_file.txt

This would prevent the file a_particular_file.txt from being extracted from the zip file.

How to List the Contents of a Zip File

To view a list of the contents of a zip file use the -l option with the zip command:

The output lists the files within the test.zip folder.

List of files within test.zip.

Extract a ZIP File to a Different Directory

To specify that you want to unzip the files to a different destination than the directory you are in, type the command:

unzip test.zip –d /home/user/destination

The –d switch tells the system to put the unzipped files somewhere else. You can substitute the path to a location of your choice for /home/user/destination.

This article has explained how to unzip and access the content of zip files in Ubuntu Linux.

Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.

This article lists the most commonly used commands and tools to remove unwanted files and directories from Linux.

Creating a file in Linux might seem straightforward, but there are some surprising and clever techniques.

Источник

Оцените статью
Adblock
detector