This article will give you detailed instructions on how to decompress a zip file in a Linux terminal. That's a very simple operation and can be done with just a single command.
For that purpose, we can use the "unzip" tool. There is a chance that it's already installed on the system that you have. However, if it's not there, you can add a relevant package from the command line as well.
Following is the command that will get you this tool on Ubuntu, Debian or Linux Mint systems:
sudo apt install unzip
To install the same utility on the RedHat, CentOS or Fedora operating systems, you may run such a command:
sudo dnf install unzip
For the OpenSUSE Linux, the command will look like the following:
sudo zypper install unzip
Now, once you have the unzip tool installed on your computer, you can try to unpack the archive. The usage is very simple. If the zip file is in the current folder, then you only have to pass the filename as an argument to the unzip command. Please see an example below:
However, if you run that command, the files will be saved into the active folder. But, if it's not the desired action, you can specify the target folder by using the -d option.
unzip -d /home/user/desired/path archive.zip
Unzipping multiple files
You may have many files in a single folder. Unpacking each of them manually is a time consuming and not a convenient task. You can unpack all the files by running a very simple command. Please see it below.
There is a pattern with a wildcard character and an extension. Such an argument instructs the utility to select all the files with ".zip" extension from the current folder.
Unzipping password protected file
If someone gave you a password protected file, that's not an issue at all. There is a possibility to decrypt it on the command line as well. You just have to use the -P option and specify the valid password. The rest will be handled by the unzip tool.
unzip -P your-password archive.zip
But please pay attention that using a password in a terminal is not a secure operation. Storing the password in the script file is not a safe option as well.
Disabling output for the unzip command
By default, the unzip tool is very verbose. It prints to the screen status for each file that is being unpacked. If you wish to disable that, you may use the single -q flag. It tells the tool to be quiet and not to output anything.
Such an option can be useful when unpacking many files or writing an automated script. Please see our example below:
unzip -q archive.zip
Exclude some file or folder when unzipping
You may want to exclude some files when unpacking a zip file. For example, this can help to speedup the decompression process if the archive contains some large file inside.
There is an option for that case in the unzip tool. You have to use the -x flag and pass all the files that must be skipped. See the example below.
unzip archive.zip -x file-to-exclude1 file-to-exclude2
It's also possible to exclude folders when extracting a zip archive. See the following command that shows how to do that:
unzip archive.zip -x folder-to-exclude1 folder-to-exclude2
Overriding existing files
There may be a case when you are extracting a zip file for the second time. That could be a new version of the archive with some modified files inside.
However, what should you do if there are already extracted files on disk? Removing them manually may be a time consuming task.
To solve such an issue, you may use the -o flag. It tells the unzip tool to override all existing files on the disk. Please see an example below.
unzip -o archive.zip
Skip existing files
Some of the files may already exist in the target folder. For example, you've already extracted the archive some time ago.
However, if you unpack the zip file and want to keep the old files, you have to use the -n flag. In that case, the unzip tool will not overwrite existing data on the disk.
Please see how to use it below:
unzip -n archive.zip
Listing files in an archive
Before decompressing the file, you may want to see what's inside of the archive. The -l flag may help in that case. If it's specified, the unzip tool will print a list of the files that are stored in a zip file.
Following is our example of how to use that flag.
unzip -l archive.zip
It's important to note that the output will also contain the metadata, such as date, time, etc. Please see what is printed to the console on the Ubuntu Linux:
developer@developer-pc:~/upload$ unzip -l my-files.zip Archive: my-files.zip Length Date Time Name --------- ---------- ----- ---- 0 2022-04-02 13:08 data/ 0 2022-04-02 13:08 data/sample/ 846 2022-04-02 13:08 data/sample/log1.dat 122 2022-04-02 13:08 data/sample/log2.dat 170616 2022-04-02 13:08 data/document.pdf 43568 2022-04-02 13:08 data/information.xls --------- ------- 207252 6 files
How to test the compressed archive
There is an option in the unzip tool that allows us to check if the archive is valid. If you specify the -t flag, the file will be decompressed into memory. After that, the CRC checksum will be calculated and compared to the value stored in the archive. If they match, the file is ok.
Please see the usage below:
unzip -t archive.zip
Such a verification may be useful in some conditions. For example, the file has been transferred over the unstable network. After that, if you are not sure that the archive is complete, you can test it.
If no data is corrupted, then the output may be like the following:
developer@developer-pc:~/upload$ unzip -t my-files.zip Archive: my-files.zip testing: data/ OK testing: data/sample/ OK testing: data/sample/log1.dat OK testing: data/sample/log2.dat OK testing: data/document.pdf OK testing: data/information.xls OK No errors detected in compressed data of my-files.zip.
Other options which are available in the unzip tool
-b - The tool will not perform text conversion. Each file must be considered as it is saved in binary form.
-D - If this option is set, the timestamps will not be taken from the archive.
-j - Use this option if you need to extract all the files into the current folder. Full paths will be ignored.
-v - Print additional information to the screen, such as compression method, date, time, size, CRC-32, etc.
Using 7z tool
The unzip tool is very widely used. However, there is an alternative utility that you can run in the command line to unpack the files.
You may consider installing the 7z package on your system. That tool is more advanced and supports additional file formats.
Please see the following command. It will get this utility for the Ubuntu operating system.
sudo apt install p7zip-full
For the Fedora system, the installation process may look like the following:
sudo dnf install p7zip-full
Also, we'll show the installation command for the OpenSUSE Linux:
sudo zypper install p7zip-full
Now, once the tool is on your system, you can unpack the file. The usage is very simple, you just have to pass the x argument and provide a file. Please see an example below.
7z x archive.zip
Additional command line parameters for the 7z tool
a - This option should be used if you want to add some file or folder to an existing archive. For example: 7z a archive.zip your-file.txt.
b - That's a very simple command that allows you to measure the speed of the CPU. In addition, it prints information about the CPU vendor, RAM size, etc.
d - If you need to remove some files from an existing archive, then this option can help you. The usage is very simple: 7z d archive.zip your-file.txt.
e - This command will unpack the archive. But please be careful with using this option, since all the files will be placed into the single directory. If you do want to keep the full paths, then you have to use the x option. It is described below.
i - If you pass this option, then the 7z tool will print a list of all the supported file formats. This can help to verify if your archive can be processed or not.
l - This command can be used to see what is inside the archive. The full list of files will be printed to the Linux terminal.
rn - Allows you to change the name of some file or folder inside of an archive.
x - This command allows you to unpack the archive. Please pay attention that the full path will be used when saving the files.
Note: There are many other options available in the 7z tool. To get a detailed and more advanced description of all the commands, please refer to the official documentation of the 7z utility.
As you see, it's very easy to extract files from an archive on the Linux operating system. You just have to install the right tool, pass the options, run the command and the files will be unpacked.
In addition, you can choose from using either the unzip tool or the more advanced 7z utility. In both of them, there are various advanced options that can help in any situation. However, these tools are the most popular and widely used. There are more open-source and proprietary software packages available that can help working with the archives.