ZIP is the most popular and widely used to compress files and directories without losing the quality of the file. It is a container which containing single or multiple compressed files or directories. In this tutorial, you will learn how to unzip files and directories in Linux systems using unzip utility.
Unzip is a utility that helps you list, test and extract compressed ZIP archives.
Install Unzip on Ubuntu and Debian
sudo apt install unzip
Install Unzip on CentOS and Fedora
sudo yum install unzip
How to Unzip File in Current Directory
If you wants to unzip the compressed files in current directory then it’s very simple. Remember, you should preserve ownership and write permission for current directory.
The above command will unzip the
bkpfile.zip file to the current directory and saves extracted files.
Unzip Files in Specific Directory
If you wants to extract zip file to a specific directory then you can pass
-d option along with unzip command. It must required write permission for a given specific directory so it is recommended that to run it with
For example, we have a file named
bkpfile.zip and wants to extract at
/var/www/html directory then you should run below command:
sudo unzip bkpfile.zip -d /var/www/html/
How to Unzip File with Password
When your zip file contains sensitive information then it is compressed using
password. So you can extract password protected file using unzip command. Below is the syntax to unzip using password:
sudo unzip -P PASSWORD bkpfile.zip
Where, PASSWORD should replace with the password of your zip file.
For example, your zip named
bkpfile.zip is password protected and your password is
xyz#123 then you can unzip using below command:
sudo unzip -P xyz#123 bkpfile.zip
Exclude Files when Unzipping a ZIP File
When you have need to extract all the files from ZIP file except any one or multiple then you should use
unzip filename.zip -x file-to-exclude
For below example, we are extracting all files and directories from the ZIP archive except the
unzip bkpfile.zip -x ".git/"
How to Overwrite Existing Files when using Unzip
Once you have extracted a zip file and you execute same command again then what should happen? By default, unzip will ask you whether you would like to overwrite all file, skip extraction of the current file, skip extraction of all files, or rename the current file.
Archive: bkpfile.zip replace server.php? [y]es, [n]o, [A]ll, [N]one, [r]ename:
If you are
sure from first and want to overwrite file then you can use
-o switch as below:
unzip -o bkpfile.zip
Note: If you have made changes in old files and you overwrite then you will lost changes.
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:
It will extract all the zip files in current directory.
You learned how to Unzip archives using the unzip command. To create a ZIP archive on a Linux system, use the Zip command.
If you have any question or suggestion, please leave comment below.
Leave a Reply