ZIP is the most widely used archive file formats, supports lossless data compression. ZIP file containing one or more containers of compressed data file or directory.
In this tutorial, we'll show you how to use the unzip command to extract the files from the command line Linux system.
What is unzip?
Unzip is a utility that can help you list, test and extract compressed ZIP archive.
Installation Unzip
By default, most Linux distributions are not installed Unzip, but you can use the distribution's package manager to install it easily.
Mounted on the decompression and Debian
sudo apt install unzip
Mounted on and decompress
sudo yum install unzip
How to decompress ZIP files
Use it without any options is the simplest form, the unzip command from the specified ZIP archive to extract all the files in the current directory.
unzip filename.zip
In order to be able to extract the ZIP archive in a particular directory, the user needs to have write access to the directory.
ZIP file does not support Linux-style ownership information, and all extracted files will have the user running the command.
For example, if you download the Wordpress installation ZIP file (https://wordpress.org/latest.zip). To extract the file into the current directory, simply run the following command:
unzip latest.zip
How to suppress the output of unzip commands
By default, unzip command prints the names of all the files it extracts as well as a summary of when extraction is complete.
-Q option can be disabled to print these messages.
unzip -q filename.zip
How to ZIP files to another directory
To unzip the ZIP file to a different directory to the current directory, use the -d option.
unzip filename.zip -d /path/to/directory
For example, to decompress the archive Wordpress latest.zip to / 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, we log the user does not write access to / var / www directory. When using sudo unzip the ZIP file, extract the files and directories owned by the user root.
How to decompress ZIP file is password-protected
To unzip the file is password protected, use the -P option followed by the password.
unzip -P PasswOrd filename.zip
How to exclude files when decompressing ZIP files
If you want to extract the ZIP archive all files except one file, use the -x option.
unzip filename.zip -x file-to-exclude
In the following example, we will extract all files and directories except .git directory of the ZIP archive:
unzip filename.zip -x "*.git/*"
How to overwrite an existing file using the Extract
Assume you have extracted the ZIP file, when you run the same command again.
unzip latest.zip
By default, Unzip asks whether you want to overwrite the current file, overwrite all files, skip to extract the current file, skip to extract all the 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 option:
unzip -o filename.zip
Be careful with this option. The file will be overwritten if you make any changes to the file, the changes will be lost.
How to decompress ZIP files without overwriting an existing file
Let's say you've unzipped a ZIP file and you make changes to certain files, but you accidentally deleted some files. You want to keep your changes and restore deleted files from the ZIP archive.
In this case, you want to use the -n option to skip the extraction of 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 simply use a command to decompress all the files:
unzip '*.zip'
Note * .zip single quotes around it. If you forget to reference parameters, shell expands wildcard, you will receive an error.
How to list the contents of Zip files
To list the contents of the ZIP file, use the -l option.
unzip -l filename.zip
In our example, we list all the WordPress installation files by executing the following command:
unzip -l latest.zip
The output is shown below:
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
in conclusion
You have learned how to extract the ZIP file and unzip the most common options. To create a ZIP archive on a Linux system, you need to use the zip command.