Open In App

Unzip Command in Linux

Last Updated : 15 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

As an open-source operating system, Linux presents plenty of powerful and versatile instructions for dealing with files and directories. One such command that performs an important role in coping with compressed files is the "unzip" command.

Compressed files are a common way to keep space and share data efficiently. In Linux, the 'unzip' command involves the rescue when you need to extract documents from ZIP files. This article will explain the basics of the 'unzip' command, its syntax, options, and practical examples.

What is Unzip Command?

The `unzip` is a command in Linux that is used to extract files from ZIP archives, preserving directory structures and permissions. It supports selective extraction of files or entire archives with offers the options for listing contents and testing archive integrity. The `unzip` is essential for managing and extracting compressed files efficiently in Linux environments.

Syntax

The simple syntax of the 'unzip' command:

unzip [options] filename.zip
  • unzip: This is the command itself, indicating the movement to unzip or extract documents from a ZIP archive.
  • [options]: Optional parameters that adjust the conduct of the 'unzip' command. These options can include flags inclusive of -l to list the contents of the ZIP record without extracting them or -d specifying the destination directory for the extracted files.
  • filename.Zip: The call of the ZIP archive document from which documents will be extracted. This must be replaced with the actual name of the ZIP file you need to unzip.

Options in the unzip Command in Linux

Here are the most commonly used options in Unzip Command in Linux:

Options

Description

Syntax

-l

Lists the contents of the ZIP file without extracting them. It provides an introduction to files and directories in the archive, together with permissions, compression ratio, and amendment timestamps.

unzip -l [filename.zip]

-d dir

Specifies the destination directory listing for extracting documents. This choice allows customers to choose a particular listing in which the contents of the ZIP record may be extracted.

unzip -d [target_directory][filename.zip]

-q

Operates in quiet mode, suppressing normal output for the duration of extraction. This is beneficial when you want to extract files without displaying unnecessary information.

unzip -q [filename.zip]

-o

Overwrites documents without prompting for confirmation. This option is available whilst you need to extract documents and replace current files without guide intervention.

unzip -o [filename.zip]

-p

Specifies a password for encrypted ZIP files. When handling password-protected archives, use this option to offer the desired password during extraction.
unzip -P [your_password] [filename.zip]

-e

Extracts files from the ZIP archive even while keeping the directory structure. This is useful if you want to keep the equal directory hierarchy as in the archive.

unzip -e [filename.zip]

-t

Tests the integrity of the ZIP archive, checking for any mistakes or corruption. It verifies whether or not the archive is legitimate before intending to extract.

unzip -t [filename.zip]

-u

Updates current files and adds new files from the ZIP archive. It guarantees that the handiest changed or new documents are extracted, keeping off pointless duplication.

unzip -u [filename.zip]

How to install Unzip Command?

The following are that guides you on how to install the unzip command in linux:

Step 1: Install Using Package Manager

  • Firstly update the software and then install the unzip software with respective package manager of the system.
  • The following are the command to install in the debian or ubuntu:
sudo apt update
sudo apt install unzip
  • The following are the commands to install in the centos/rhel OS:
sudo yum install unzip
  • The following are the command used to install in the pacman for arch Linux:
sudo dnf install unzip

Step 2: Verify Installation

  • Verify the installation of unzip software with the following command:
unzip -v

Step 3: Usage

  • The following command is used for unzipping the archived files. The command looks as follows:
unzip archive.zip

Examples and Usage of Unzip Command in Linux

The following are the some of the examples of unzip command in linux:

1. Basic Extraction

To perform basic extraction of a ZIP file, use the command unzip 'filename.zip'. This command extracts all documents from the specified ZIP archive, putting them inside the cutting-edge operating listing. It is the simplest syntax for decompressing and retrieving the contents of a ZIP file in Linux.

Syntax

unzip [filename.zip]

Example

  • Suppose you have a file named 'archive.zip' that you want to extract which contains two files inside i as "file1.txt and file2.txt". We need to unzip it in the current directory.

Syntax and Output

unzip archive.zip
  • This command will help extract the contents of "archive.zip" into the current directory. The output displays each file as it is extracted.
unzip archived files

2. Extract to a Specific Directory

  • To extract files to a specific directory, utilize the '-d' option followed by the desired destination path:

Syntax

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

Example

  • Suppose you possess a file named 'archive.zip,' and you intend to extract its contents into the directory '/path/to/destination.'

Syntax and Output

unzip archive.zip -d /path/to/destination
  • Here, the extraction is directed to the specified '/path/to/destination,' such as the Desktop. The command ensures that files from 'archive.zip' are placed in the designated directory.
Extracting to specific directory

3. List Contents

  • To view the contents of a ZIP file without extracting, apply the '-l' option:

Syntax

unzip -l filename.zip

Example

  • Suppose Imagine you want to inspect the contents of 'archive.zip' without executing the extraction process.

Syntax and Output

unzip -l archive.zip
  • In this scenario, the '-l' option provides a list of files contained within 'archive.zip' without initiating the extraction, allowing you to preview the file structure.
List Contents

4. Quiet Mode

  • Execute file extraction quietly by using the '-q' option:

Syntax

unzip -q filename.zip

Example

  • If you want to extract files from 'archive.zip' without displaying any messages.

Syntax and Output

unzip -q archive.zip
Quiet Mode
Quiet Mode
  • Here, The zip file is in the home section, That's why it is also extracted in the home section.
Quietly Extracted in Home
  • Here, the zip file is located in the home section, and extraction occurs in the same location. The command ensures a quiet extraction process, suppressing informational messages.

5. Overwrite Files

  • Overwrite current files without the affirmation of the usage of the '-o' option:

Syntax

unzip -o filename.zip

Example

  • Extract files from 'archive.zip' and overwrite existing files without prompting for confirmation.

Syntax and Output

unzip -o archive.zip
  • The '-o' option facilitates the overwriting of files from 'archive.zip' without requiring explicit confirmation, streamlining the extraction process.
Overwrite files

6. Password-Protected ZIP

  • If the ZIP file is password-protected, utilize the '-P' option to specify the password:

Syntax

unzip -P password filename.zip

Example

  • Suppose 'archive.zip' is protected with the password 'secure123.' Use the following syntax to extract its contents.

Syntax and Output

unzip -P secure123 archive.zip
  • Here, the ZIP file necessitates the password 'secure123' for extraction. The '-P' option enables you to provide the password directly in the command line.
Password protected zip

7. Encryption and Security

  • To extract encrypted files and maintain security, appoint the '-e' alternative:

Syntax

unzip -e filename.zip

Example

  • If 'archive.zip' incorporates encrypted files, use the '-e' alternative for stable extraction.

Syntax and Output

unzip -e archive.zip
  • The '-e' option ensures the secure extraction of encrypted files from 'archive.zip,' maintaining the confidentiality and safety of the contents.
Encryption and  Security

8. Update Existing Files

  • Update existing files during extraction using the '-u' option, the syntax of this looks as follows:

Syntax

unzip -u filename.zip

Example

  • If you want to update existing files from 'archive.zip' without overwriting newer variations, appoint the '-u' option.

Syntax and Output

unzip -u archive.zip
  • The '-u' choice updates files from 'archive.zip' best if a more recent model is available, heading off overwriting more modern files with older ones.
Update existing file

9. View Compression Information

  • Retrieve compression-associated details during extraction using the '-Z' option:

Syntax

unzip -Z filename.zip

Example

  • If you wish to look/inspect compression-related information while extracting files from 'archive.zip.'

Syntax and Output

unzip -Z archive.zip
  • The '-Z' choice provides details about the compression techniques hired in 'archive.zip' at some stage in the extraction technique, supplying insights into the report compression techniques used.
View Compression Information

Features of Unzip Command

The following are the features of unzip command:

  • Extracting Files: The unzip command facilitates with allowing us to extract the files and directories from zip archives. It helps in preserving their original structure and permissions.
  • Support for Various Formats: It supports various zip formats, including ZIP, ZIP64, encrypted zip files (with password support), and archives spanning multiple disks.
  • Listing Contents: You can list the contents of a zip file using unzip -l, which provides detailed information about files within the archive.
  • Recursive Extraction: The unzip can recursively extract nested zip files within an archive, extracting all contained files and directories.

Conclusion

In conclusion, the 'unzip' command is a flexible tool for dealing with compressed documents in Linux. Armed with the information of its primary syntax and options, you can efficiently extract and control ZIP archives on your command-line adventures. As you test with the 'unzip' command, you may benefit from self-belief in dealing with compressed files like a Linux pro.


Next Article

Similar Reads