Open In App

Unzip Command in Linux

Last Updated : 19 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
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 newbie-pleasant guide will walk you through the basics of the ‘unzip’ command, its syntax, options, and practical examples.

Syntax of Unzip Command

The simple syntax of the ‘unzip‘ command is:

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]

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.

Basic Extraction

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

Extracting to the 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

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

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

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

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

Encryption and Security

8. Update Existing Files

Update existing files during extraction using the ‘-u‘ option:

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

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

View Compression Information

Frequently Asked Questions on unzip command – FAQs

Can I extract multiple ZIP files at once?

Yes, you could extract multiple ZIP files by providing their names sequentially within the command.

How do I extract files to the current directory?

If no destination is precise with ‘-d’, the files might be extracted to the current directory.

Can I unzip a password-protected ZIP file?

Yes, use the ‘-P’ option followed by the password to extract files from a password-protected ZIP archive.

Are there other compression formats supported by the unzip command?

The unzip command primarily supports ZIP files. For other formats like tar or gzip, you may need to use different commands (tar, gzip, etc.).

Can I update an existing ZIP file with new files?

Yes, you can use the -u option to update an existing archive: unzip -u existing.zip newfile.txt.

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads