How to Unzip Files in Linux
5 min read
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 is not installed by default in most Linux distributions, but you can easily install it using the package manager of your distribution.
unzip on Ubuntu and Debian
sudo apt install unzip
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 extracts all files from the specified ZIP archive to the current directory.
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:
ZIP files do not support Linux-style ownership information. The extracted files are owned by the user that runs the command.
You must have write permissions on the directory where you are extracting the ZIP archive.
Suppress the Output of the
unzip 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
Unzip a ZIP File to a Different Directory
To unzip a ZIP file to 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
because usually 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 are owned by the user root.
Unzip a Password Protected ZIP file
To unzip a file that is password-protected, invoke the
unzip command with the
-P option followed by the password:
unzip -P PasswOrd filename.zip
Typing a password on the command line is insecure and should be avoided. A more secure option is to extract the file normally without providing the password. If the ZIP file is encrypted,
unzip will prompt you to enter the password:
archive: filename.zip [filename.zip] file.txt password:
unzip will use the same password for all encripted files as long as it is correct.
Exclude Files when Unzipping a ZIP File
To exclude specific files or directories from being extracted, use the
-x option followed by space-separated list of archive files you want to exclude from extracting:
unzip filename.zip -x file1-to-exclude file2-to-exclude
In the following example we are extracting all files and directories from the ZIP archive except the
unzip filename.zip -x "*.git/*"
Overwrite Existing Files
Let’s say you’ve already unzipped a ZIP file and you are running the same command again:
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. If you made any changes to the files, the changes are lost.
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, use the
-n option which forces
unzip to skip the extraction of a file that already exists:
unzip -n filename.zip
Unzip Multiple ZIP Files
You can use regular expressions to match multiple archives.
For example, 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.
List the Contents of a Zip File
To list the contents of a ZIP file, use the
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
is a utility that helps you list, test, and extract compressed ZIP archives.
To create a ZIP archive on a Linux system, you’ll need to use the zip command .
Feel free to leave a comment if you have any questions.