Unzip is the command-line utility in Linux operating systems to extract the contents of the zip file. This command allows the users to uncompress the zip files and bring them back to their original version.
The practice of compressing the files using the zip format is adopted usually when large files are supposed to be sent through email. Also, the files which are not in use can be compressed to utilize the space for other applications.
This blog walks through the usage methods of unzip command-line utility to uncompress the zip files in Linux.
What is the unzip Command-line Utility in Linux?
The unzip command-line utility is used to uncompress the ZIP archives and to extract its contents. This command-line utility also supports different options that help the users to handle the zip archive files conveniently.
How to Install unzip Command-line Utility in Linux?
The unzip command-line utility comes pre-installed in most of the Linux distributions including Debian, Ubuntu, and Linux Mint.
If it is not pre-installed, then use one of the below commands according to the installed Linux distribution.
Install unzip Command-line Utility in Ubuntu/Debian
To install the unzip command-line utility in Debian and Ubuntu-based Linux distributions:
$ sudo apt install unzip -y
Install unzip Command-line Utility in CentOS/RHEL
If you are using the CentOS/RHEL Linux distributions, then run the command:
$ sudo yum install unzip
Install unzip Command-line Utility in Arch Linux
In Arch Linux, it can be installed using the Pacman package manager:
$ sudo pacman -S unzip
Install unzip Command-line Utility in Fedora
Use the dnf package manager, unzip command can be installed on Fedora:
$ sudo dnf install unzip
Install unzip Command-line Utility in OpenSUSE
Execute the below-mentioned command, and install the unzip command-line utility in OpenSUSE:
$ sudo zypper install unzip
How to Find the Version of the unzip Command-Line Utility?
To find the installed version of the unzip command-line utility on Linux distribution, use the command:
$ unzip -v
What is the unzip Syntax in Linux?
To use unzip in Linux, use the below-mentioned usage syntax of unzip command-line utility:
$ unzip [options] [filename.zip] [specify files or directories]
The explanation of the general syntax of unzip command:
- Use the “unzip” to invoke the unzip command-line utility
- Use different options of unzip to perform different functions
- Mentioned the zip file name to uncompress it
- Specify the specific file or folder within the zip archive file to extract it. Else it will extract all the contents of the zip compress file.
How to unzip a Zip File from the Terminal?
For the understanding of unzipping the zip file using the command line, use the above-mentioned general syntax.
Some of the basic usage of the unzip command in Linux have been explained with examples.
Example 1: List Down the Contents using the unzip Command
Unzip command can be used to list down the contents of the zip archive file. For example, using the zip command, the contents of the “itslinuxfoss.zip” will be displayed:
$ unzip -l itslinuxfoss.zip
The “l” option of the unzip is used to list down the contents of the compressed zip archive file.
Example 2: Unzip a Single File Using the unzip
To extract the specific single file from the zip archive, follow the general syntax of unzip command. For example, to extract the “itslinuxfoss/myfile5.txt” from “itslinuxfoss.zip”, run the command:
$ unzip itslinuxfoss.zip itslinuxfoss/myfile5.txt
Example 3: Unzip the Multiple Files Using the unzip
Now extract the multiple files from the “mylinuxfoss.zip” using the unzip command:
$ unzip itslinuxfoss.zip itslinuxfoss/myfile6.txt itslinuxfoss/myfile6.txt
Example 4: Unzip a Compressed File From Another Directory Using the unzip
To unzip the compressed zip archive located in some other directory, use the “d” option of the unzip command. For example, to unzip the “itslinuxfoss.zip” from the “Downloads” directory, execute the command:
$ unzip itslinuxfoss.zip -d /home/vboxuser/Downloads
Example 5: Unzip the Entire Zip Archive Using the unzip Command
To unzip the complete zip archive, for example, “itslinuxfoss.zip”, run the command:
$ unzip itslinuxfoss.zip
Example 6: Unzip All the Zip Archives within the Directory
To unzip all the zip archives within the same directory, simply execute the command:
$ unzip *.zip
Example 7: Unzip a Zip Archive without Retaining its Structure
To extract the compressed file without retaining the structure of its directory, use the “j” option:
$ unzip -j itslinuxfoss.zip
Example 8: Overwrite the Existing Files while Unzipping
When the same file is overwritten again, a message for the confirmation of the overwrite is prompted on the screen:
$ unzip itslinuxfoss.zip
To overwrite the file without displaying the confirmation message, use the “o” option of unzip command:
$ unzip -o itslinuxfoss.zip
Example 9: Extract a Zip Archive that is Password Protected
Use the “p” option of the unzip command to unzip the password protected zip archive. After using the “p” option, type the “password” and then the compressed file name. For example, to unzip the “itslinuxfoss” protected with a “qwer1234” password, use the command:
$ unzip -p qwer1234 itslinuxfoss.zip
Example 10: Suppressing the Output Allows to Unzip the Zip Archive
Use the “q” option to unzip a zip file without printing any messages:
$ unzip -q itslinuxfoss.zip /home/vboxuser/Downloads
How to Unzip a File in Linux Using the p7zip or 7z?
Another command utility that can be used to unzip the compressed files in Linux is by using the 7z command. The 7z command utility can be installed with the p7zip package, with the command:
$ sudo apt install p7zip-full -y
After installing the 7z command utility, list down the contents of the zip archive using its “l” option:
$ 7z l itslinuxfoss.zip
To unzip the contents of the “itslinuxfoss.zip” with the 7z command, use its “x” option:
$ 7z x itslinuxfoss.zip
How to Unzip a File Using a Python Script?
The Python programming language can also be used to unzip a file in Linux and to use Python programming, create a Python script by opening a file using the nano text editor:
$ nano pyunzip_script.py
The Python script listed below should be copied and pasted into the newly opened file:
#!/usr/bin/env python3
import sys
from zipfile import PyZipFile
for zip_file in sys.argv[1:]:
pzf = PyZipFile(zip_file)
pzf.extractall()
Using the keyboard shortcuts CTRL+S to save a file and CTRL+X to close the nano text editor, respectively. Now to unzip the “itslinuxfoss.zip” using the Python script, run the command:
$ python3 pyunzip_script.py itslinuxfoss.zip
To confirm the successful execution of the Python script, list down the contents of the directory:
$ ls
The folder of “itslinuxfoss” has successfully extracted using the Python script.
How to Use a Perl Script to Unzip a File on Linux?
The “Perl Programming Language” is an additional programming language that may be used to unzip a file in Linux. It can be used similarly to the Python language, first create a new file:
$ nano perlunzip_script.pl
Copy and paste the below-mentioned Perl script for unzipping a zip archive file:
#!/usr/bin/perl
use strict;
use warnings;
my $zip_file = shift;
unless ($zip_file) {
die "Usage: $0 <zip_file>\n";
}
unless (-e $zip_file) {
die "Error: $zip_file not found.\n";
}
my $unzip_command = "unzip $zip_file";
system($unzip_command) == 0 or die "Error while unzipping: $!\n";
print "Successfully extracted $zip_file.\n";
Now using the shortcuts of CTRL+S and CTRL+X, save the file and close the nano text editor respectively. Now make the Perl script executable using the command:
$ chmod +x perlunzip_script.pl
To unzip an “itslinuxfoss.zip” using the Perl script, run the command:
$ ./perlunzip_script.pl itslinuxfoss.zip
The zip archive file has successfully unzipped with the Perl script.
These are all many ways to use a terminal or the command-line to unzip a file in Linux.
Conclusion
To unzip a file in Linux, two different commands including unzip and 7z command utilities can be used. Python and Perl programming scripts can also be used. This blog has explained all the mentioned methods to unzip a file in Linux using a terminal. Also, it explained the general usage syntax of the unzip command utility with 7+ examples.