Zip is one of the most popular archives or compression formats and algorithms. The zip is used to compress data into a single file. Multiple files or folders can be zipped into a single zip file. In this tutorial, we examine different cases to unzip files in Linux.
Install unzip Command
In most of the distributions the
unzip command is not installed by default. But we can install the unzip easily using the apt and dnf package managers for distributions like Debian, Ubuntu, Mint, Kali, Fedora, RHEL, and CentOS.
Debian, Ubuntu, Mint, Kali:
$ sudo apt install unzip
Fedora, RedHat, CentOS:
$ sudo dnf install unzip
We start with a simple example. We unzip or decompress the zip file named
wordpress.zip . We only use the unzip command with the zip file name without any extra parameters.
$ unzip wordpress.zip
We can see that all extracted files and folders are displayed in the output.
During extraction of the zip file a lot of output is created to list extracted files and folders. If we need to suppress these outputs we can use the
-q option. The -p option is the short form of the quiet.
$ unzip -q wordpress.zip
Unzip To Specified Directory
The unzip command extracts files and folders to the current working directory by default. But we can specify a different path or directory to extract the zip file with the unzip command. The
-d option is used to specify the path or directory we want to unzip. The path can be relative or absolute. In the following example, we extract the contents of the zip file into the “/var/www”.
$ unzip wordpress.zip -d /var/www
Unzip Password Protected File
The zip files can be protected with passwords by encrypting them and securing them from unauthorized access. In order to see the contents of the zip file or extract the contents of the zip file with password protection, we should provide the password. The
-P option should be used to provide the password to decompress password protected zip file.
$ unzip -P P3AssW0rD wordpress.zip
Exclude Files and Folders
The unzip command extracts all content (files and folders) by default as we expect. But we can exclude some files or folders from the extraction using the unzip command. The
-x option can be used to exclude files and folders from extraction. In the following example, we exclude the folders named plugin and tmp.
$ unzip wordpress.zip -x /plugin /tmp
We can also exclude specific file extensions from unzipping operations. In the following example, we exclude the
$ unzip wordpress.zip -x *.txt
Overwrite Existing Files
When we try to unzip files that already exist the unzip command asks every existing file to overwrite. If there are a lot of files this can be a very tedious task to accept every overwrite operation. The
-o option is used to overwrite existing files.
$ unzip wordpress.zip -o
Unzip Without Overwriting Existing Files
Another case is when unzipping files we may want to unzip files without overwriting simply skipping already existing files. the
-n option is used to unzip files without overwriting.
$ unzip -n wordpress.zip
Unzip Multiple Zip Files
The unzip command can be used to unzip multiple files. Just provide the zip files named below.
$ unzip wordpress.zip drupal.zip
Alternatively, we can only specify the
*.zip extension in order to unzip all zip files.
$ unzip *.zip
List Contents of Zip Files
Even the unzip command is used to unzip files it can be also used to list the contents of a zip file without unzipping it. The
-l option is used to list the contents of the specified zip file.
$ unzip -l wordpress.zip