Read in 4 minutes
How to Unzip Files in Linux
In this tutorial, we will show you 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 extract compressed ZIP archives.
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.
In order to be able to extract a ZIP archive in 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 that you downloaded the Wordpress installation ZIP file. To unzip this file to the current directory, you’d simply run the following command:
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.
-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
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
unzip filename.zip -x file-to-exclude
In the following example we are extracting all files and directories from the ZIP archive except the
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 are running the same command again.
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
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 accidently 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:
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
unzip -l filename.zip
In our example, we list all WordPress installation files by executing:
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
You have learned how to unzip ZIP files and most common Unzip options.
If you want to learn more about Unzip visit the Unzip Man page.