How to Use Unzip in Linux

Zipping and unzipping files simplifies many complex tasks, such as file transfer! You’ll learn how to use unzip with Linux commands to improve your VPS workflow in this tutorial!

Zip is a popular compression function that is portable and simple to use. You can even unzip files created in Linux in Windows!

Unzip is a utility that is not installed by default on most Linux distributions but is easily installed. You can match the .tar.gz file compression by creating.zip files!

What Is Zip Used For

Below are a few scenarios in which you may choose to use zip files:

  • When you are frequently working between Windows and Unix based systems. Not only does this compress files but also is a file package utility. Works on multiple operating systems
  • To save bandwidth. If you have limited or restricted bandwidth, then zip can be used between two servers for file transfer
  • Transfers files quickly. Zip utility reduces file size, therefore reducing transfer time
  • Upload or download directories at a faster speed
  • Save disk space
  • Unzip password protected .zip files
  • Enjoy a good compression ratio

Remember, before taking advantage of Unzip on Linux, you’ll have to SSH into your virtual private server.

How to Install Unzip

Now that we know what zip is used for, let’s go over how to install unzip in your Linux VPS based on the operating system you’re using.

How to Install Unzip in Debian and Ubuntu Systems

Installing unzip is easy! With Ubuntu and Debian use the command below to install unzip:

sudo apt install unzip

Sit back and wait a minute, until the installation is finished.

To create zip files, you’ll also have to install zip. You can do this with the following command:

sudo apt-get install zip

How to Install Unzip on Linux CentOS and Fedora

This again is simple and can be done using below command:

sudo yum install unzip

Once the installation is complete you can check the path with the following command:

which unzip

After you execute the direction in the command line, you should get an output that looks like this:

/usr/bin/unzip

You can also confirm everything is installed properly by using the command bellow. It will give a verbose with unzip utility details.

unzip -v

How to Use Zip and Unzip in Linux

Now that we know how to install the utility, we can start learning the basic uses of it:

How to Create Zip Files in Linux

The basic syntax to create a .zip file is:

zip options zipfile list_Of_files

To test this, we created two files – ExampleFile.txt and ExampleFile1.txt. We’ll compress them into sampleZipFile.zip with the following command:

zip sampleZipFile.zip ExampleFile.txt ExampleFile1.txt

How to Use Linux to Unzip a file

The unzip command can be used without any options. This will unzip all files to the current directory. One such example is as shown below:

unzip sampleZipFile.zip

This by default will be unzipped in the current folder provided you have read-write access.

How to Remove a File from a .zip File

Once a .zip file is created, you can remove or delete files in it. So, if you want to remove ExampleFile.txt from the existing sampleZipFile.zip, then you can use the following command:

zip –d sampleZipFile.zip ExampleFile.txt

Once this command is executed, you can unzip the .zip file using:

unzip sampleZipFile.zip

Over here you will find that ExampleFile.txt has been removed and can’t be seen on extraction.

How to Update Zip Files

Once a .zip file is created, you can add a new file to an existing .zip file. Suppose a new file ExampleFile2.txt needs to be added to the already existing sampleZipFile.zip. You can do this with the command shown below:

zip –u sampleZipFile.zip ExampleFile2.txt

Now if you extract sampleZipFile.zip, you will find the new file ExampleFile2.txt added to it.

How to Move a File to a Zip

You can easily move specific files to an the zip file. That means that after adding the files, they will be deleted from their original directories. This is mostly used when you have large file or directory, but need to conserve disk space. This is done by adding the -m option. A sample of this command would be:

zip –m sampleZipFile.zip ExampleFile2.txt

How to Use Zip Recursively in Linux

The -r option is used to recursively zip files. This option will compress all the files present within a folder. An example of such command is as shown below:

zip –r sampleZipFile.zip MyDirectory

In the example, MyDirectory is a directory which has multiple files and sub-directories to be zipped.

How to Exclude Files in a Zip

While creating a .zip file, you can exclude unwanted files. This is done by using the -x option. Below is an example:

zip -x sampleZipFile.zip ExampleFile.txt

Here ExampleFile.txt will not be added to the sampleZipFile.zip.

How to Unzip to a Different Directory

In case you do not want to unzip to the current directory but want to specify a directory location, then this can also be done. Use the -d option to provide a directory path in the unzip command. An example of such command is as shown below:

unzip sampleZipFile.zip -d /usr/sampleZip/ExampleDir

How to Use Linux Unzip with Multiple Zip Files

If you want to unzip multiple zip files existing within your current working directory then you can use a command as shown below:

unzip ‘*.zip’

This command will unzip all the individual zip files.

How to Suppress Output When Using Unzip in Linux

By default, when we use the unzip command, the command prints list of all the files that are getting extracted. A summary of the extraction process is printed. In case you want to suppress these messages, then you can use the -q option. The command would be as shown below:

unzip -q sampleZipFile.zip

How to Exclude Files Using Unzip in Linux

In case you want to extract all files except for one, then you can use a similar command as shown below:

unzip sampleZipFile.zip -x excludedFile.txt

Here the command will unzip all files except excludedFile.txt.

You can also prevent specific file types from getting extracted. One such example is as shown below:

unzip sampleZipFile.zip -x "*.png/*"

The above command will exclude all .png files from being extracted.

How to Use Unzip in Linux with Password Protected Files

A password protected .zip file can be decompressed using the -P option. A sample of such command is as shown below:

unzip -P Password sampleZipFile.zip

In the above command, Password will be the password for the .zip file.

How to override Zip Files

When you unzip the same file again in the same location where the file was extracted, by default you will encounter a message asking whether you want to overwrite the current file, overwrite all files, skip extraction for the current file, skip extraction for all files or rename current file.

The options would be as shown below:

[y]es, [n]o, [A]ll, [N]one, [r]ename

You can override these files by using the -o options. One such example is as shown below:

unzip -o sampleZipFile.zip

Caution should be taken while executing this command since this will completely overwrite the existing copies. Any changes made in the earlier copy will be overwritten.

How to Use Linux Unzip Without Overwriting Files

If you have unzipped a file and made some changes but you accidentally deleted a few files, then you can use this approach to restore it! Use the -n option to skip the extraction in case a file already exists. So effectively only files which do not exist will be extracted. An example of such a command is:

unzip -n sampleZipFile.zip

How to List the Content of a Zip in Linux

The -l option will list all the files within the .zip along with the timestamp and other basic details. An example of such command is:

unzip -l sampleZipFile.zip

Conclusion

That’s it, you’re introduced to all the essential functions of the zip and unzip Linux utilities. Start improving your file management right now!

Author
The author

Edward S.

Edward is a content editor with years of experience in IT writing, marketing, and Linux system administration. His goal is to encourage readers to establish an impactful online presence. He also really loves dogs, guitars, and everything related to space.