The “unzip” Command in Linux [7 Practical Examples]

Unzip is a function that is used to decompress a zip(compressed) file. To extract all the files from the zip file into the current directory of the zip file unzip command is used in Linux. In this article, I will demonstrate the syntax, options, and practical applications of the unzip command in Linux.

A. Description

The Unzip command extracts all the files & directories from the zip file in the current directory. This command also displays the files & directories it is extracting from the zip file in the terminal.

B. Syntax

The syntax for the unzip command is pretty simple. Firstly, you have to write the unzip command and then after using a space, you will write the name of the zip file you want to extract. You need to write the name of the file with the extension.

unzip [-OPTION]... [FILE]

Note: In the syntax above, OPTION is enclosed by a square bracket and followed by 3 dots representing that more than one option can be used concurrently.

C. Options

Many useful options are available for the unzip command. I have listed some of the most used ones here. However, if you want to know more about options for the unzip command you can always check the man page for the unzip command.

man unzip

Useful Options 

  • -d, Extracts files to the specified directory.
  • -l, Lists contents of zip(compressed) file.
  • -t, Checks if the zip file is a valid zip archive or not.
  • -v, Displays detailed info of each file inside a zip archive.
  • -x, Extracts only mentioned files.

Note: The options in Linux CLI (Command Line Interface) are case-sensitive, so be careful while using them.

Practical Examples of the “unzip” Command in Linux

The unzip command in Linux is an easy-to-use command which will extract the contents of the zip archive. There are so many practical applications of this command. Here you will get to know some useful examples of the unzip command.

Example 1: Extract Files Using the “unzip” Command in Linux

To decompress a file named MyZip.zip, I am using the unzip command. To do the same you can follow the procedure below:

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

unzip MyZip.zip

➌ Now, press the ENTER button.

❹Type the following command in the command prompt:

ls

This will display the list of extracted files here.

➎ Finally, press the ENTER button.

Output >

In the image below, you can see all the files inside the zip file are extracted.Extract Files Using the “unzip” Command in Linux

Example 2: Extract Files to a Specified Directory Using the “unzip” Command in Linux

Using the unzip command along with option -d, I am extracting all the files of the zip file to the Random_Directory folder. To do the same you can follow the procedure below:

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

unzip MyZip.zip -d /home/sylvie/Desktop/Random_Directory

➌ Now, press the ENTER button.

❹Type the following command in the command prompt:

ls Random_Directory/

This will show you the list of extracted files here.

➎ Finally, press the ENTER button.

Output >

As you can see in the image below, all the files inside the zip file are extracted to the specified folder.Extract Files to a Specified Directory Using the “unzip” Command in LinuxExtract Files to a Specified Directory Using the “unzip” Command in Linux

Example 3: Display the List of Contents of a Zip File Using the “unzip” Command in Linux

To display the list of all the contents of the zip file to the terminal I am using the unzip command with option -l. To do the same you can follow the procedure below:

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

unzip -l MyZip.zip

➌ Now, press the ENTER button.

Output >

So, here the output shows the contents inside the zip file.Display the List of Contents of a Zip File Using the “unzip” Command in Linux

Example 4: Extract Only the Mentioned Contents of the Zip File

You can use the unzip command with option -x to extract only the contents of the zip file you want to be extracted.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

unzip -x MyZip.zip 1.txt 2.txt

➌ Now, press the ENTER button.

❹Type the following command in the command prompt:

ls

This will display the list of extracted files here.

➎ Finally, press the ENTER button.

Output >

In the following image, you can see only the mentioned contents of the zip file are extracted.Extract Only the Mentioned Contents of the Zip File

Example 5: Display Detailed Info on Each Content of the Zip File

To display detailed information about the contents of the zip file I am using the unzip command with option -v. To do the same you can follow the procedure below:

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

unzip -v MyZip.zip

➌ Now, press the ENTER button.

Output >

As you can see, the output is displaying the details of all the contents of the zip file.Display Detailed Info on Each Content of the Zip File

Example 6: Check Whether the Zip File is a Valid Zip Archive or Not

To check whether the zip file is a valid archive or not, I am using the unzip command with the option -t. To do the same you can follow the procedure below:

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

unzip -t MyZip.zip

➌ Now, press the ENTER button.

Output >

As you can see in the following image, the output tests each zip file’s contents to see whether the zip file is a valid archive or not.Check Whether the Zip File is a Valid Zip Archive or Not

Example 7: Extract Multiple Zip Files Together Using the “unzip” Command in Linux

To extract multiple zip files together you can use the unzip command with a regular expression (“ * ”,“ ? ”,“ . ”,“ ^ ”). Here I am using “ * ” to unzip the contents of two different zip files.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

unzip "*.zip"

➌ Now, press the ENTER button.

Output >

In the image below, you can see that the two different zip files are extracted using a single command.Extract Multiple Zip Files Together Using the “unzip” Command in Linux

Conclusion

As you have seen in this article, the unzip command is one of the useful commands in Linux. You’ve also learned the syntax, some useful options, and the practical application of this command. Here, you have also seen the use of regular expressions with the unzip command in Linux. So, go through all the materials and execute accordingly to become a power user.


Similar Readings

 

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Jannatul Ferdousi Sylvie

Hi there! This is Jannatul Ferdousi Sylvie. I am one of the Linux Content Developer Executives at Softeko. I live in Dhaka, Bangladesh. I have completed my BSc. in Software Engineering from American International University-Bangladesh. You can see my projects in the GitHub account. I love traveling, shopping, cooking, and gardening. Read Full Bio

Leave a Comment