Home Icon Home Resource Centre How To Unzip A File In Linux? List Of Commands, Options & More

How To Unzip A File In Linux? List Of Commands, Options & More

Linux's unzip command is highly flexible and can be used for much more than simply extracting zip files! Learn its syntax and explore essential commands and options for diverse usage and applications.
Shivangi Vatsal
Schedule Icon 0 min read
How To Unzip A File In Linux? List Of Commands, Options & More
Schedule Icon 0 min read

Table of content: 

  • What Is Unzip Command In Linux?
  • Unzip Options In Linux
  • Installation Of Unzip In Linux
  • How To Unzip A File In Linux?
  • Unzip Commands In Linux
  • Features And Usage Of Unzip Command In Linux
  • Conclusion
  • Frequently Asked Questions
expand

Unzipping files is a common task when working with compressed data on Linux systems. The unzip command allows you to extract files from ZIP archives quickly and efficiently. Whether you're dealing with downloaded files, backups, or shared documents, knowing how to unzip a file in Linux can streamline your workflow and simplify file management.

In this article, we'll guide you through the basic steps of unzipping a file in Linux. We’ll also cover the various options, arguments, and features of the unzip command, helping you become proficient in managing ZIP archives on your Linux system.

What Is Unzip Command In Linux?

The unzip command in Linux (or a folder, etc.) is a utility used to extract files from ZIP archives. A ZIP archive is a compressed file format that can contain one or more files or directories.

In other words, this command plays a key role when we want to unzip a folder in Linux systems and extract files contained in it. It is also used to decompress these archives, allowing users to access the files inside. We will discuss how to unzip a file in Linux in detail, but first let's look at the basic command syntax and options we can use it with.

Linux Unzip Command Syntax

First of all, you need to install the unzip package. To do so, type the following command in your terminal window:

sudo apt-get install unzip

Once all the packets have been installed, to unzip the archive files, type in the following unzip command syntax in the terminal:

unzip archive.zip

Note: A more useful command line tool is 7z, which zips and unzips a range of compression formats is given below.

sudo apt-get install p7zip-full

Unzip Options In Linux

The unzip command in Linux comes with a range of options and flags that allow you to manage the extraction of files from ZIP archives. You can combine them with the command to unzip a file in Linux (or folder) for greater flexibility and versatility. Here's a list of commonly used options:

  • -l option: To list the contents of a given zip file we use the -l (list archive) option. To execute the -l option to the current file, open the windows terminal and type the following command:

$ unzip -l file.zip

  • -t option: Tests the integrity of a ZIP file without extracting its contents. It also checks for corruption or errors in the program.

$ unzip -t file.zip

  • -z option: It is used to display the ZIP file's comment. If the ZIP archive includes a comment (which is a text string associated with the archive), using this option will print it to the terminal.

$ unzip -z file.zip

  • '-o' option: If you want to overwrite existing files without prompting, it can be done using the -o option. To run the -o (overwrite) option open the terminal window and run the following command:

$ unzip -o file.zip

  • -n option: It never overwrites existing files. If the file already exists, unzip will skip extracting it.

$ unzip -n file.zip

  • -u option: It updates existing files if they are older than the corresponding files in the ZIP archive.

$ unzip -u file.zip

  • -q option: Quiet mode; It suppresses output messages, providing a quieter extraction process.

$ unzip -q file.zip

  • -v option: Verbose mode; provides a verbose listing of the contents of the ZIP file, including more details like compression ratios.

$ unzip -v file.zip

  • -qq option: Very quiet mode; it suppress all output, including errors. To execute this option to the current file, open the windows terminal and type the following command:

$ unzip -qq file.zip

  • -x <pattern>: Excludes specific files or current working directories from extraction based on a pattern.

$ unzip file.zip -x "*.txt"

  • -j option: Junk paths; extracts all files into the current directory path, ignoring any directory structure within the archive.

$ unzip -j file.zip

  • -C option: Matches filenames case-insensitively during extraction. To execute this option to the current file, open the windows terminal and type the following command:

$ unzip -C file.zip

  • -a option: It automatically converts text files to Unix format. This can be useful when extracting ZIP files created on different operating systems.

$ unzip -a file.zip

  • '-d' option: It extracts the files to the specified directory. If the directory does not exist, it will be created.

$ unzip file.zip -d /path/to/destination

  • -p option: Specifies the password to unlock a password-protected file. However, using the '-p' option is not recommended since it exposes the password in plain text on your terminal window.

$ unzip -p file.zip | less

Installation Of Unzip In Linux

The unzip utility is commonly pre-installed on many Linux distributions. However, if it isn't available on your system, you can easily install it using your distribution's package manager. Here are the steps to install unzip on various Linux distributions:

Install Unzip On Debian-Based Distributions (Ubuntu, Linux Mint, etc.)

To install unzip in Linux, you can use the apt package manager and type the following commands:

sudo apt update
sudo apt install unzip

Install Unzip On Red Hat-Based Distributions (Fedora, CentOS, RHEL, etc.)

To install unzip on these distributions, you can use the dnf (or yum on older systems) package manager and type the following commands:

sudo dnf install unzip
# or for older versions
sudo yum install unzip

Installing Unzip From Source

If you prefer to compile unzip from the source, you can download the source code from the official Info-ZIP website and follow these general steps:

  1. Download the source code tarball.

  2. Extract the contents.

  3. Navigate to the extracted current working directory.

  4. Run the following commands in the terminal:

make -f unix/Makefile generic
sudo make -f unix/Makefile install

Verifying Installation

After installation, you can verify that unzip is installed and check the version with:

$ unzip -v

This command will display the version of unzip installed on your system, confirming that the installation was successful.

How To Unzip A File In Linux?

Here's a step-by-step guide on how to unzip a file in Linux:

  • Open the terminal: First, open your terminal application. This is the command-line interface where you'll run the unzip command.
  • Install unzip (if necessary): If the unzip command is not installed on your system, you can install it using your package manager. You can check if unzip is installed on your system by running the following command:

unzip -v

  • Navigate to the directory containing the ZIP file: Use the cd command to change to the current directory where your ZIP file is located. For example:

cd /path/to/your/zipfile

Replace /path/to/your/zipfile with the actual path where your ZIP file is stored.

  • List contents of the ZIP file (Optional): If you want to see the contents of the ZIP file before extracting, use the -l option:

unzip -l filename.zip

  • Unzip the file: To extract the contents of the ZIP file, use the unzip command followed by the name of the ZIP file. For example:

unzip filename.zip

Replace filename.zip with the actual name of your ZIP file. This command extracts all the files and directories from the ZIP archive into the current directory.

  • Extract to a specific directory: If you want to extract the files to a specific directory, use the -d option followed by the destination directory:

unzip filename.zip -d /path/to/destination

  • Extracting specific files:  To extract only specific files from the ZIP archive, list the filenames after the archive name:

unzip filename.zip file1.txt file2.txt

  • Exclude specific files: To exclude certain files from extraction, use the -x option: 

unzip filename.zip -x "*.jpg"

  • Verify the ZIP file integrity (Optional): To test the integrity of the ZIP file without extracting, use:

unzip -t filename.zip

Unzip Commands In Linux

Some of the most commonly used unzip commands in Linux are listed below.

  • file[.zip]: Specifies the path to the ZIP archive(s). When a wildcard is used in the filename, the operating system determines the order in which matching files are processed.
    Note that the path itself cannot contain wildcards; only the filename can. For example-

$ unzip archive.zip
$ unzip /path/to/archives/*.zip

  • [file(s)]: A list of specific archive members to extract, separated by spaces. Regular expressions (wildcards) can be used to match multiple members. For example-

$ unzip archive.zip file1.txt file2.txt
$ unzip archive.zip "*.txt"

  • [-x xfile(s)]: An optional list of archive members to exclude from extraction. Wildcards can be used, and this option can help prevent the extraction of certain files or file types. For example-

$ unzip archive.zip -x file1.txt
$ unzip archive.zip -x "*.jpg"

  • [-d exdir]: Specifies the target directory for extracted files. By default, files are extracted to the current directory. The -d option allows specifying a different directory. For example-

$ unzip archive.zip -d /path/to/destination

This option can be placed anywhere on the command line: before the ZIP file specification, immediately after it, or after the list of files to extract or exclude.

Features And Usage Of Unzip Command In Linux

The command to unzip a file in Linux (folder) offers a range of features and flexible usage options for extracting, listing, testing, and managing files within ZIP files. Here are some of its common features:

  • Extraction of Files: The primary function of unzip is to extract files from ZIP archives. It can extract entire archives or specific files and directories.

  • Listing Contents: You can list the contents of a ZIP archive without extracting the files, allowing you to see what's inside before deciding to extract.

  • Selective Extraction: The unzip command in Linux also supports selective extraction, allowing you to extract only specific files or directories from an archive.

  • File Overwrite Control: The command provides options to overwrite existing files, skip extraction of existing files, or update only newer files.

  • Testing Archives: The unzip command in Linux can test the integrity of a ZIP archive file format to ensure that it is not corrupted.

  • Password Protection: It can handle encrypted ZIP files and allows you to specify a password for the extraction process.

  • Case Sensitivity: You can choose to treat filenames case-insensitively during extraction.

  • Output Control: The unzip command in Linux offers options to control the verbosity of its output, including quiet modes to suppress output.

  • Conversion Options: It can convert text files to the local standard format during extraction.

  • Handling of File Paths: The command can extract current files with full paths, junk the paths (extract only the files), or extract files into a specified directory.

Conclusion

The unzip command in Linux is a versatile and essential tool for managing ZIP archives. Whether you're extracting a single file, handling complex directory structures, or working with large datasets, unzip offers a range of options to tailor the extraction process to your needs. From basic usage (like how to unzip a file in Linux) to advanced options like excluding specific files or specifying an output directory, understanding the unzip commands can streamline your workflow and ensure efficient data management. 

Frequently Asked Questions

Q. How do I extract all files from a ZIP archive using the unzip command?

To extract all files from a ZIP archive, you can use the command:

unzip filename.zip

Replace filename.zip with the name of your ZIP file. This will extract the contents into the current directory.

Q. Can I extract files to a specific directory using the unzip command?

Yes, you can extract files to a specific directory by using the -d option:

unzip filename.zip -d /path/to/destination/

Replace /path/to/destination/ with the path to the directory where you want to extract the files.

Q. What is the unzip command in Linux?

The unzip command in Linux is a utility used to extract current files from a ZIP archive. It supports various options for extracting files, viewing the contents of a ZIP file, and even testing the integrity of the ZIP file.

Q. How can I list the contents of a ZIP file without extracting them?

To list the contents of a ZIP file without extracting them, you can use the -l option:

unzip -l filename.zip

This will display a list of files contained in the compressed ZIP archive along with their original file sizes and timestamps.

Q. What should I do if a file already exists in the destination directory?

If a file already exists in the destination directory, the unzip command will prompt you for confirmation before overwriting it. You can use the -o option to overwrite files without prompting:

unzip -o filename.zip

Alternatively, you can use the -n option to never overwrite existing files:

unzip -n filename.zip

Suggested Reads:

  1. How To Open A File In Linux Using 3 Different Approaches
  2. 60+ Most Asked UNIX Interview Questions And Answers
  3. How To Delete A File In Linux | How To Remove A Directory
  4. How To Create A File In Linux: Touch, Cat, Echo, Printf Command
  5. Linux Kernel In A Nutshell To Help You Prepare For Linux Interview Questions
Edited by
Shivangi Vatsal
Sr. Associate Content Strategist @Unstop

I am a storyteller by nature. At Unstop, I tell stories ripe with promise and inspiration, and in life, I voice out the stories of our four-legged furry friends. Providing a prospect of a good life filled with equal opportunities to students and our pawsome buddies helps me sleep better at night. And for those rainy evenings, I turn to my colors.

Tags:
Computer Science Engineering

Comments

Add comment
comment No comments added Add comment
Powered By Unstop Logo
Best Viewed in Chrome, Opera, Mozilla, EDGE & Safari. Copyright © 2024 FLIVE Consulting Pvt Ltd - All rights reserved.