How to Unzip a File in Linux – A Step-by-Step Guide

How to Unzip a File in Linux – A Step-by-Step Guide

4 mins read111 Views Comment
Updated on Dec 14, 2023 20:21 IST

Unzipping files in Linux is crucial for handling compressed data efficiently. It enables easy access to a variety of archived files, simplifying storage and sharing. Whether extracting specific files to designated directories, handling password-protected archives securely, or managing file overwrites cautiously, mastering the unzip command empowers users to efficiently manage and access data within ZIP archives on their Linux systems.

2023_03_How-to-Unzip-a-File-in-Linux.jpg

The most popular format for archives that allows lossless data compression is ZIP. One or more compressed files or directories are in a ZIP file, a data container.

In this article, we’ll go through how to use the unzip command on a Linux command line to unzip files.

Explore Online Linux Courses

Setting up unzip (Installation)

Most Linux distributions do not have unzip installed by default, but you may quickly add it using your distribution’s package manager.

Here is how you can install unzip on Debian and Ubuntu.

2023_03_image-27.jpg

 

 

 

Also explore: How to list files in Linux?

Unzipping a ZIP file


When invoked without any options, the unzip command extracts all files from the supplied ZIP archive to the current directory in its most basic form.

Let’s use downloading the WordPress installation ZIP file as an example. To unzip this file to the current directory, use the following command:

2023_03_image-28.jpg

ZIP files do not support ownership information in the Linux manner. The user who executes the command is the owner of the extracted files.

The directory where you are extracting the ZIP package has to have write rights.

Suppress the unzip command’s output.


Unzip, by default, prints a summary when the extraction is complete, along with the names of all the files it is extracting.
To prevent the publishing of these messages, use the -q flag.

2023_03_image-29.jpg

To a Different Directory, Unzip a ZIP File


Use the -d option to unzip a ZIP file to a location other than the current one:

2023_03_image-30.jpg

 

 

 

 

For instance, to unzip the latest.zip WordPress archive into the /var/www/ directory, use the command:

2023_03_image-31.jpg

Because we are currently signed in as a user that typically lacks write access to the /var/www directory, sudo is used in the command above. When decompressing ZIP files with sudo, the root user owns the recovered files and folders.

Unzip a password-protected ZIP File


Invoke the unzip command with the -P option and the password to unlock a password-protected file:

2023_03_image-32.jpg

Password input on the command line is unsafe and ought to be avoided. Extracting the file normally without entering the password is a more secure alternative. If the ZIP file is protected with encryption, unzip will request the password:

2023_03_image-33.jpg
2023_03_image-34.jpg

Unzip will utilize the password for all encrypted files as long as the password is entered correctly.

While Unzipping a ZIP file, exclude certain files.


Use the -x option and a list of archive files you want to prevent from extracting, separated by spaces. This is as follows.

2023_03_image-35.jpg

The .git directory is the only directory from the ZIP archive that is not being extracted in the example below:

2023_03_image-36.jpg

Replace Any Existing Files


Consider the following scenario: You are performing the same command after previously unzipping a ZIP file:

2023_03_image-37.jpg

By default, unzip will ask whether you want to rename the current file, rename all files, skip extraction of all files, overwrite only the current file, or overwrite all files.

2023_03_image-38.jpg

Use the -o option if you wish to overwrite existing files without asking:

Take care when choosing this option. All modifications you made to the files are gone.

2023_03_image-39.jpg

Unzip a ZIP file without overwriting existing files.


Imagine you’ve already unzipped a ZIP file, made some modifications, and unintentionally erased certain files. The files have been removed from the ZIP bundle, and your changes should be restored.


Use the -n option to tell unzip to forgo extracting a file that already exists in this situation:

2023_03_image-40.jpg
How to Find Files in Linux?
How to Find Files in Linux?
The find command in Linux is powerful tool that allows to quickly and easily scan through the file system to find files and directories. In this article, we will discuss...read more
How to Set Environment Variables in Linux?
How to Set Environment Variables in Linux?
Here you will understand how to set environment variables in Linux. We have covered topics such as setting, unsetting, listing environment variable etc. Let’s explore.
How to Delete a File in Linux?
How to Delete a File in Linux?
Need to clear some space on your Linux system? It’s easier than you might think! With a simple command, you can swiftly delete any file. But what’s the right way...read more

Unzip a number of ZIP files


To match several archives, utilize regular expressions.
You may unzip all the files with only one command. For instance, if you have several ZIP files in your current working directory:

2023_03_image-42.jpg

Take note of the single quotes that surround *.zip. The shell will extend the wildcard character if you fail to quote the argument, resulting in an error.

List the Zip File’s Contents


Use the -l option to list a ZIP file’s contents:

2023_03_image-41.jpg

Explore: Top Linux Interview Questions and Answers

Conclusion

You can list, examine, and extract compressed ZIP archives with the aid of the program unzip. You must use the zip command on a Linux system to generate a ZIP archive.

Contributed By: Sangeetha Bandari

FAQs

Why doesn't my Linux system have unzip pre-installed?

Linux distributions often exclude unzip by default to keep the base system lightweight. You can easily install it using your distribution's package manager, such as 'apt' for Debian-based systems or 'yum' for Red Hat-based systems.

Is it possible to unzip files to a specific directory in Linux?

Yes, it's possible. Use the '-d' option followed by the directory path to specify where to extract the contents of the ZIP file. For example:

unzip file.zip -d /path/to/directory
Copy code
.

How can I unzip a password-protected ZIP file safely?

Safely unzip a password-protected file by using the '-P' option followed by the password. However, avoid entering passwords directly on the command line for security reasons. Instead, let the unzip command prompt you to enter the password securely.

What is the risk of using the -o option to overwrite files during unzipping?

The '-o' option in unzip overwrites existing files without confirmation, potentially causing data loss. It's important to exercise caution while using this option as it might replace modified files, leading to unintended loss of changes.

Can I extract multiple ZIP files using a single command in Linux?

Yes, you can extract multiple ZIP files using wildcards and a single unzip command. For instance, unzip '*.zip' will extract all ZIP files in the current directory. Ensure to enclose the wildcard pattern in single quotes to prevent shell expansion errors.

 

 

 

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio