How to decompress zip file in Linux

ZIP is the most widely used archive file format and supports lossless data compression. ZIP file is a data container containing one or more compressed files or directories

In this article, I will show you how to use the unzip command to decompress files in the Linux system through the command line

Unzip command can help you list, test and extract compressed ZIP archives

Install unzip

By default, Unzip is not installed in most Linux distributions, but you can install it using the distribution’s package manager.

Install unzip on Ubuntu and Debian

$ sudo apt install unzip

Install unzip on CentOS and Fedora

$ sudo yum install unzip

How to decompress ZIP files

It is the simplest form to use it without any options. This unzipcommand will extract all files from the specified ZIP archive to the current directory.

$ unzip filename.zip

In order to be able to extract the ZIP archive to the specified directory, the user needs to have write access to the directory

ZIP files do not support Linux ownership information, and all extracted files will be owned by the user who runs the command

For example, suppose you downloaded the WordPress installation ZIP file. To extract this file to the current directory, just run the following command:

$ unzip latest.zip

How to not display the information output of the unzip command

By default, the unzip command prints the names of all files it extracts and a summary when the extraction is complete

Use the -qoption to disable printing of these messages.

$ unzip -q filename.zip

How to extract ZIP files to other directories

To unzip the ZIP file into a different directory, use this -doption

$ unzip filename.zip -d /path/to/directory

For example, to unzip the WordPress archive latest.zipto a /var/www/directory, you would use the following command

$ sudo unzip latest.zip -d /var/www

In the above command we use sudo because in most cases, the user we log in does not have /var/wwwwrite permission to the directory. When using sudo to decompress the ZIP file, the extracted files and directories will be owned by the user root.

How to unzip a password-protected ZIP file

To decompress a password-protected file, use the -Poption followed by the password.

$ unzip -P PasswOrd filename.zip

How to exclude files when extracting ZIP files

If you want to extract all but one file from the ZIP archive, use the -xoption

$ unzip filename.zip -x file-to-exclude

In the following example, we will extract .gitall files and directories except directories from the ZIP archive

$ unzip filename.zip -x "*.git/*"

Use to overwrite existing files when decompressing

Suppose you have unzipped a ZIP file and you run the same command again

$ unzip latest.zip

By default, Unzip will ask you whether you want to overwrite the current file, overwrite all files, skip the extraction of the current file, skip the extraction of all files, or rename the current file

Archive:  latest.zip
replace wordpress/xmlrpc.php? [y]es, [n]o, [A]ll, [N]one, [r]ename:

If you want to overwrite an existing file without prompting, use the -ooption

$ unzip -o filename.zip

Please use this option with caution. The file will be overwritten, if you make any changes to the file, the changes will be lost

How to unzip ZIP files without overwriting existing files

Suppose you have unzipped a ZIP file, and you made changes to some files, but you accidentally deleted some files. You want to keep the changes and restore the deleted files from the ZIP archive.

In this case, you will use the -nforced decompression option to skip the extraction of the existing files.

$ unzip -n filename.zip

How to decompress multiple ZIP files

If there are multiple ZIP files in the current working directory, you can decompress all files with only one command:

$ unzip '*.zip'

Note the surrounding single quotes *.zip. If you forget to quote the parameter, the shell will expand the wildcard

How to list the contents of a Zip file

To list the contents of the ZIP file, use the -lswitch.

$ unzip -l filename.zip

In our example, we list all WordPress installation files by executing the following command:

$ unzip -l latest.zip

The output will look like this

Archive:  latest.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2018-08-02 22:39   wordpress/
     3065  2016-08-31 18:31   wordpress/xmlrpc.php
      364  2015-12-19 12:20   wordpress/wp-blog-header.php
     7415  2018-03-18 17:13   wordpress/readme.html
...
...
    21323  2018-03-09 01:15   wordpress/wp-admin/themes.php
     8353  2017-09-10 18:20   wordpress/wp-admin/options-reading.php
     4620  2017-10-24 00:12   wordpress/wp-trackback.php
     1889  2018-05-03 00:11   wordpress/wp-comments-post.php
---------                     -------
 27271400                     1648 files

So far you have learned how to decompress ZIP files and the most common decompression options. If you have any questions, please leave a message in the comments below

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Related Articles