Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Apr 12, 2024
Difficulty: Easy

Unzip Command in Linux

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

The Unzip Command in Linux is a handy tool that is used to efficiently handle the download and storage of data and files in Linux and, in fact, in other operating systems. Zipping the files reduces the time to download files over networks by compressing them into .zip extension files. The Unzip command lets you get the actual files from the .zip files.

unzip command in linux

Unzip command saves a lot of time and resources because you can unzip the zipped files when you need, and the zip files will take less space until unzipped and can be easily shared because of their lesser size, which can later be unpacked using unzip command.

What is the Unzip command in Linux?

The Unzip command allows you to unzip or unpack the zip extension files to get the actual files that were originally zipped or compressed. Doing this has many advantages, as we discussed above. Zipping and unzipping are the new norms because multiple files or directories can be compressed into a single zip file, which we can later unzip to access the original files and directories.

Linux Unzip Command Syntax

The basic syntax of using unzip in Linux is following:


unzip {example.zip} 

This command will read the example.zip file and unpack the contents to get the original contents that were zipped into this file. It gets unzipped into the current working directory.  Let's take a straightforward example to understand this command further.

For example, we have a sample.zip file and want to unzip it. So the Linux command for this will be:

unzip sample.zip 

This will unzip the contents of the sample.zip file into the current working directory.

You can use other options to work with unzip commands more efficiently. Let's look at them in brief.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example of Unzip Command in Linux

Now, let us see some examples of using the Unzip Command in Linux

  • Simple Unzip
    We have a folder in which we have opened our bash terminal and want to unzip demo in it. We achieve this using the following command:
$ unzip demo.zip


OUTPUT
We will see that the folder has been unzipped.

demo folder unzipped
unzip command
  • Extracting Specific Files
    We can also unzip some specific files using the Unzip Command in Linux. For example, we have a pom.xml file we want to unzip in the demo. We can achieve this using the following command:
$ unzip demo.zip pom.xml


OUTPUT
We will see that only pom.xml is extracted.
 

extracting specific file
  • Extracting from any location
    We can extract our zipped files from any location providing its correct path. Refer to the following command for our above-mentioned example:
$ unzip demo.zip -d C:\Users\jatin\OneDrive\Desktop\w\demo.zip

 

We use the -d option for it.

OUTPUT

The folder is extracted.

extracting with the path

Options in Unzip Command in Linux

You can specify many options to modify the unzip command you are writing. Let's take a look at the most useful ones:

Option

Usage

-dUsed to define destination directory to unzip to
-lTo list the contents of the zip file we are unzipping
-oUsed to override the already existing files on the system
-xUsed to exclude files while unzipping
-PUsed when we try to unzip password protected zip files
-qUsed to suppress all progress and output indicators

Modifiers of unzip

Modifiers are the options that can be used with the unzip command. These are used to extract files from a compressed ZIP archive. Some of the most commonly used modifiers are discussed below: 

Modifiers

Usage

-aIt is used to convert text files. The extract file is exactly extracted as they are saved. 
-bAll the overwritten files backup copy is saved. 
-CWhen selecting an archive entry from the terminal list of selection patterns, it uses a case-sensitive matching technique.
-DFor extracted files, timestamp restoration is skipped.
-jJunk paths is represented.  
-nNo existing file is overwritten. 
-oThe existing file is overwritten without permission. 
-qOperations are performed quietly. 

How to Use Unzip Command on Ubuntu 20.04?

To use the Unzip Command in Ubuntu 20.04, first of all, install the unzip package using the following command:

sudo apt-get install unzip

OR

sudo apt install unzip

 

Now, that you have installed the package, you can start using the Unzip Command. Like, for simple extraction you can use the following command:

unzip demo.zip

 

Or, if you want to extract and list the contents of it, you can use the -l option:

unzip -l demo.zip

Use Cases of using Unzip command in Linux

There are many use cases for the Unzip Command in Linux; let's have a closer look at each of them-

1. Unzipping files into different directories

When you write the simple unzip syntax and don't specify the directory you want the contents to go to, it goes to the current working directory. There is an option, however, to specify the same directory where you want the unzipped content to get to. Using the”-d” options and selecting the path to the required directory, your content will get unzipped there. For example:

unzip sampleText.zip -d /home/user/desktop/myTextFiles

This will send the unzipped content from the sampleText.zip file to the myTextFiles folder on the desktop.

2. Unzipping multiple files at once

While unzipping saves a lot of time than traditional methods of downloading and sharing, there are even faster ways to unzip your files. You can do this by putting all your .zip extension files into a folder and then using the following command:

unzip ‘*.zip’

This command will unzip all the files with the .zip extension in your directory. This is very helpful for extracting contents from multiple zip files simultaneously.

3. Overriding Zip Files

When you Unzip files that already exist in the exact location again, you will be prompted with messages whether to override the old files or rename the new files and store them or not to override and drop the command or override none.

The Options are:

Option

Usage

[y]Yes,override the file
[n]No, dont override the file
[N]None, skip
[re]Rename, rename the unzip copy, and save


The additional -o option will override all the files.Caution is necessary while using this command because this will completely override the old files. All the changes you made to the earlier copies will be overwritten.

For example:

unzip -o sample.zip

This will override the older copies where you previously unzipped this sample.zip file.

4. Unzipping password-protected Files

Sometimes, the user that created the zip file may have secured it using a password or access key to access these files; you can run the following command.

unzip -P randomPassword sample.zip

This command will take in the password “randomPassword” and then unzip the sample.zip file if it matches.

Must Read, gzip command in linux

Advantages of Unzip Command in Linux

Let's look at the various advantages of Unzip in Linux:

  • CrossPlatform compatibility: Unzip and Zip are actively supported on multiple operating systems, making file transfer easy and efficient. It reduces the network demand for transfer because the compressed files are smaller.
     
  • Password Protection: While using Zip and Unzip, you can control access to the zipped content using a password, using which only someone can unzip the file.
     
  • Handling Large Archives: When zipped, the file size reduces considerably, and we can now handle large archives more easily. This zip and unzip mechanism allows us to transfer huge amounts of data easily.
     
  • More control over data: Because of the command line-friendly commands in unzip, we have more control over what data should be unzipped and what should be excluded; as we saw above, we can exclude selective files if we want.
     
  • File Compression: During zipping, the file size reduces considerably, and data becomes more manageable because, using unzip, we can easily get back the original files.

Limitations of Unzip Command in Linux

  • Limited Error reporting: When we encounter any error while Unzipping, the Unzip command doesn't provide enough information about the error, like the file that caused it or the error code.
     
  • Limited supported file formats: The unzip command helps decompress or unpack files of .zip format; it is not useful while unzipping files that have been compressed in other formats like RaR, tar.gz, 7z, etc.
     
  • Lack of File Integrity Verification: The "unzip" command does not perform integrity checks on extracted files by default. It does not verify the authenticity or integrity of the compressed data during extraction. 
     
  • Potential File Overwrite: By default, the "unzip" command prompts for confirmation when extracting files that already exist in the destination directory. This can lead to accidental overwriting of files if not carefully managed.
     
  • Dependency on External tools: Sometimes, the unzip doesn't come with built-in Linux systems, so you have to install some dev tools for this to work.

Frequently Asked Questions

What is the unzip command?

Zipping the files reduces the time to download files over networks by compressing them into .zip extension files. The Unzip command lets you get the actual files from the .zip files. Hence, it is valid.

How do I unzip a file in Linux?

You can use the command unzip archive.zip in your terminal in Linux if you are at the location where the zipped file is present. Or, you can use the -d option to unzip it from anywhere by providing the specific path.

How do I unzip a zip GZ file in Linux?

You can use the gunzip file.zip command to unzip a GZ file in Linux. This way, the file is decompressed and then the actual file is available to you in an unzipped form.

How to unzip file in Linux SSH?

In Linux SSH, first connect to your server using the command: ssh <username>@<ip-address> -p <port number>. Then, you can install the unzip package and use the Unzip Command in Linux SSH.

How do I unzip a Unix command?

To unzip a file in UNIX based operating system use the unzip command. Choose the correct directory and use the unzip command with the correct zip file name. This will extract all the files and directories present in the zip folder in the correct directory. 

How do I unzip a file in Terminal?

The steps to unzip the file in the terminal remain the same. Open the terminal in a Unix-based operating system (such as Linux or macOS) and choose the directory in which the zip file is saved. Use the unzip command, and also remember that the unzip command is case sensitive so ensure the file name is written correctly. 

Conclusion

From the above article, you can infer that the Unzip Command in Linux is an extremely powerful and effective tool when it comes to data transfer and even storage in some cases. This command is very easy to use and can be very helpful in various situations. It has a variety of options that allow you to control how the files are uncompressed. Want to learn more about Linux and other important commands? Don't worry Coding Ninjas have you covered. Refer to the following article: Linux - Commands List,Basic File Management Commands in Linux.

Happy Learning!

Topics covered
1.
Introduction
2.
What is the Unzip command in Linux?
2.1.
Linux Unzip Command Syntax
3.
Example of Unzip Command in Linux
4.
Options in Unzip Command in Linux
5.
Modifiers of unzip
6.
How to Use Unzip Command on Ubuntu 20.04?
7.
Use Cases of using Unzip command in Linux
7.1.
1. Unzipping files into different directories
7.2.
2. Unzipping multiple files at once
7.3.
3. Overriding Zip Files
7.4.
4. Unzipping password-protected Files
8.
Advantages of Unzip Command in Linux
9.
Limitations of Unzip Command in Linux
10.
Frequently Asked Questions
10.1.
What is the unzip command?
10.2.
How do I unzip a file in Linux?
10.3.
How do I unzip a zip GZ file in Linux?
10.4.
How to unzip file in Linux SSH?
10.5.
How do I unzip a Unix command?
10.6.
How do I unzip a file in Terminal?
11.
Conclusion