1. Introduction

When working on compressed files, especially in the .zip format, we often need to extract their contents to access individual files and directories. Files that are compressed can conserve valuable storage space and facilitate efficient data transfer by reducing the size of the data.

With the unzip command, we can easily access the content of .zip files.

In this tutorial, we’ll explore the unzip command and its various options.

2. Installing unzip Utility

The unzip utility doesn’t come pre-installed on many Linux distributions, so we need to install it first to extract files from .zip archives:

$ sudo apt install unzip     # For Debian-based distributions
$ sudo yum install unzip     # For RPM-based distributions
$ sudo pacman -S unzip       # For Arch Linux-based distributions

We can choose any of the commands depending on the Linux distribution and package manager.

3. Basic Use of the unzip Command

To begin with, let’s explore some simpler uses of unzip.

3.1. Single File Extraction

When we use the standalone unzip command, it extracts the content of the specified zip file in the same working directory as that file:

$ unzip test_file.zip
Archive: test_file.zip
extracting: file1.pdf
extracting: file2.pdf
extracting: file3.pdf

In other words, the files and directories archived in test_file.zip are restored to their original form and placed in the current working directory.

3.2. Multifile Extraction

We can also use the unzip command to extract multiple zip files using the wildcard (*) operator:

$ unzip '*.zip'
Archive:  file2.zip
 extracting: file1.txt
 extracting: file2.txt
 extracting: file3.txt

Archive:  file1.zip
 extracting: file1.pdf
 extracting: file2.pdf
 extracting: file3.pdf

2 archives were successfully processed.

In certain situations, we may receive password-protected zip files to ensure the confidentiality and integrity of their content. This way, only authorized users with the correct password can access the content by providing the correct password:

$ unzip password_file.zip
Archive: password_file.zip
[password_file.zip] file1.pdf password:my_password
extracting: file1.pdf
extracting: file2.pdf
extracting: file3.pdf

While the basic unzip command is sufficient for extracting the content of a zip file, there are various options we can use to customize the unzipping process.

3.3. List the Content Without Extracting

When dealing with multiple zip files, we can preview their content as a list using the -l option.

This feature enables us to verify the required files and other objects within the zip file before extraction:

$ unzip -l test_files.zip

Sometimes, the output can be overwhelming if the archive contains a large number of files or a complex directory structure. To quickly locate the exact file we need, we can combine the -l option with the grep command:

$ unzip -l project_files.zip | grep report.pdf

This command pipes (|) the output of the unzip command to grep, which then searches for the string report.pdf. We can see the relevant line on the terminal if the grep command finds it. Otherwise, the output displays nothing.

3.4. Extracting Files to a Different Directory

Also when working with one or more compressed files, we often want to extract their contents into a separate directory. This approach keeps the files organized and prevents clutter from accumulating in the current working directory.

To achieve this, we can use the -d option and specify the targeted directory where we want to extract the files.

For example, to extract the content of test_file.zip into a directory named extracted_files, we can use the basic syntax with -d:

$ unzip -d extracted_files test_file.zip
Archive:  test_file.zip
 extracting: extracted_files/file1.pdf
 extracting: extracted_files/file2.pdf
 extracting: extracted_files/file3.pdf

Similarly, we can extract content from multiple zip files:

$ unzip -d extracted_files '*.zip'

These two commands extract the content to a subdirectory in the same working directory.

However, we can provide a complete path to extract them in a different directory:

$ unzip -d /home/Documents/extracted_files test_file.zip
Archive: test_file.zip
extracting: /home/Documents/extracted_files/file1.pdf 
extracting: /home/Documents/extracted_files/file2.pdf 
extracting: /home/Documents/extracted_files/file3.pdf

After execution, the command first creates the extracted_files directory if it already doesn’t exist and extracts the content inside it.

3.5. Extracting Specific Files Only

When extracting a zip file, we may not always need to extract the entire archive. Instead, we can retrieve specific files or particular groups of files and objects such as documents.

To achieve this selective extraction, we can use the -x option followed by the file names or patterns we want to exclude:

$ unzip test_file.zip -x "*.ini" "*.cfg"

The command extracts all files from test_file.zip except those with .ini and .cfg extension.

Alternatively, we can use the -i option to include only some files or patterns for extraction:

$ unzip archive.zip -i "*.txt" ".docx"

Thus, the -x and -i options filter out the specific files we want to extract providing precise control over the extraction process.

3.6. Extracting Files by Suppressing the Output

Until now, we’ve seen that extracting a zip file also displays its content on the terminal. This way, we can track the extraction process and its progress.

However, when we already know the file content or when extracting large archives, we may want to suppress the output.

To achieve this, we utilize the -q option:

$ unzip -q test_file.zip

This command extracts the content of test_file.zip without displaying any output.

3.7. Extracting Files by Allowing Duplication

When we use any of the above-discussed options, we get a prompt whether we want to overwrite existing files or not if any duplicate file is found.

So, we can use the -o option if we’re fully aware of the situation and want to allow the duplication:

$ unzip -o test_file.zip

This command automatically overwrites any existing files with the same name.

In addition, we can also combine the discussed options to further increase the precision and performance of the unzip command.

4. Conclusion

The unzip command is a powerful tool for handling compressed files.

In this article, we first explored how to install the unzip utility in different Linux distributions. Later, we discussed the extraction process using the standalone unzip command and also saw its various options to fine-tune the output.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments