ZIP is the most widely used archive file format that supports lossless data compression. A ZIP file is a data container containing one or more compressed files or directories.
In this tutorial, we will explain how to unzip files in Linux systems through the command line using the unzip
command.
What is Unzip?
unzip
is a utility that helps you list, test and extracts compressed ZIP archives.
Install Unzip
unzip
is not installed by default in most Linux distributions, but you can easily install it using the package manager of your distribution.
Install unzip
on Ubuntu and Debian
sudo apt install unzip
Install unzip
on CentOS and Fedora
sudo yum install unzip
How to Unzip a ZIP file
In it’s simplest form when used without any option, the unzip
command will extract all files from the specified ZIP archive to the current directory.
unzip filename.zip
To extract a ZIP archive into a specific directory, the user needs to have write permissions on that directory.
ZIP files do not support Linux-style ownership information, and all extracted files will be owned by the user that runs the command.
As an example, let’s say you downloaded the WordPress installation ZIP file. To unzip this file to the current directory, you’d simply run the following command:
unzip latest.zip
How to Suppress the Output from the unzip
Command
By default the unzip
command prints the names of all the files it’s extracting and a summary when the extraction is completed.
Use the -q
switch to suppress the printing of these messages.
unzip -q filename.zip
How to Unzip a ZIP file to a Different Directory
To unzip a ZIP file in a different directory than the current one, use the -d
switch:
unzip filename.zip -d /path/to/directory
For example, to unzip the WordPress archive latest.zip
to the /var/www/
directory, you’d use the following command:
sudo unzip latest.zip -d /var/www
In the command above we are using sudo
because in most cases the user we are logged in as doesn’t have write permissions to the /var/www
directory. When ZIP files are decompressed using sudo, the extracted files and directories will be owned by the user root.
How to Unzip a Password Protected ZIP file
To unzip a file that is password-protected, use the -P
switch followed by the password:
unzip -P PasswOrd filename.zip
How to Exclude Files when Unzipping a ZIP File
If you want to extract all the files from a ZIP archive except for one, use the -x
switch:
unzip filename.zip -x file-to-exclude
In the following example we are extracting all files and directories from the ZIP archive except the .git
directory:
unzip filename.zip -x "*.git/*"
How to Overwrite Existing Files when using Unzip
Let’s say you’ve already unzipped a ZIP file and you are running the same command again:
unzip latest.zip
By default, unzip
will ask you whether you like to overwrite only the current file, overwrite all files, skip extraction of the current file, skip 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 existing files without prompting, use the -o
switch:
unzip -o filename.zip
Use this option with caution. The files will be overwritten and if you made any changes to the files, the changes will be lost.
How to Unzip a ZIP File Without Overwriting Existing Files
Let’s say you’ve already unzipped a ZIP file and you made changes to some files but you accidentally deleted few files. You want to keep the changes and to restore the deleted files from the ZIP archive.
In this case, you’d use the -n
option which forces unzip
to skip the extraction of a file that already exists.
unzip -n filename.zip
How to Unzip Multiple ZIP Files
If you have multiple ZIP files in your current working directory you can unzip all files using only one command:
unzip '*.zip'
Note the single quotes around the *.zip
. If you forgot to quote the argument the shell will expand the wildcard character and you will get an error.
How to List the Contents of a Zip File
To list the contents of a ZIP file, use the -l
switch:
unzip -l filename.zip
In the example below, we are listing all WordPress installation files:
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
Conclusion
We’ve talked about how to unzip files in Linux, from the command line.
To create a ZIP archive on a Linux system, you’ll need to use the zip command.