How to Zip and Unzip Files in Linux

File compression is a powerful mechanism to save storage space and facilitate easy file transfers, especially when the file size is huge. There are various formats of compressed files such as .zip, .rar, etc. But, zip remains the most commonly used format. If you’re on a Linux system, zipping and unzipping files (compressing and decompressing) is a task that you can accomplish from the command line interface as well as the GUI. So, in this article, we have discussed how to zip and unzip files in Linux using these two methods.

For those unaware, zipping means compressing a file or multiple files of different formats into a single file, such that it occupies less space and can be transferred using less bandwidth. Unzipping a file means extracting all the compressed files from a zip file. That said, let’s look at how you can zip and unzip files in Linux:

How to Zip Files in Linux (GUI Method)

The GUI method to zip and unzip files is easier compared to the CLI method but lacks various options and features which may be useful for many users. The method shown here will work for all Linux distributions with any file manager. For this tutorial, we are using the Nautilus file manager in Ubuntu 20.04 LTS version.

1. First, open any file manager of your choice and navigate to the directory where your files are located.

2. Then, select the files you want to add to the zip file.

selecting multiple files to zip in Linux

3. Right-click on any one of the selected files and select “Compress.”

4. Here, you need to give your compressed file a new name and select the file format – .zip, .7z or .tar.xz. Finally, click on “Create” at the top right.

5. And that’s it. The compressed file will be saved to the same directory with the selected format.

How to Unzip Files in Linux (GUI Method)

There are three different ways to unzip an archive file using the GUI File manager on Linux:

1. Extract All Files in the Same Directory

  • First, open the file manager and locate the archive file.
  • Then, right-click on the file and select “Extract Here.” This will extract your files into a new sub-directory with the same name as the archive file.

2. Unzip All Files in a Different Directory

  • First, open the file manager and locate the archived file.
  • Then, right-click on the file and select the “Extract To” option. It will open a new dialogue box.
  • Select the path to which you want to unzip all the files from the left pane and click on “Select.”
  • All your files will be extracted in a new sub-directory with the same name as the archived file.

3. Unzip Only Selected Files

  • First, open the file manager and locate the archive file. Then, double-click on the file. This will open the Archive Manager.
  • Select multiple files by holding down the “CTRL” key while clicking on the file names you want to select. Then, click on “Extract” in the top left corner.
  • In the new dialogue box, choose the path and click on “Select” in the top right corner.
  • All your files will be extracted in a new sub-directory with the same name as the archived file.

How to Zip Files Using Linux Command Line

The CLI method of compressing and decompressing files is not only faster compared to the GUI method, but it also provides a lot of options to tinker with files. While there are multiple commands to compress and decompress files, zip and unzip are the most common commands as they are cross-platform and have wide online support.

1. Install Zip and Unzip Packages

By default, both the packages – zip ad unzip – come pre-installed on most distros. Use the commands below to install them, if it is not available by default:

sudo apt install zip

sudo apt install unzip

2. Compress Files Using Zip Command in Linux

The basic syntax to compress files using the zip command is as follows:

zip <option> file.zip file(s), where

  • <option> is used to specify various options to compress files into zip.
  • file.zip is the final name of the zip file after compressing. Here, giving the .zip extension is optional.
  • file(s) is the file name which we want to compress.

Some of the common options to pair with the zip command are:

OptionsDescription
-dUsed to remove a file from the zip archive.
-uUsed to add new files to a zip file.
-mThis option will delete the original files after zipping them.
-rIt is used to zip an entire directory.
-xThis option is used to exclude the files while zipping.

3. Zip a Single File in Linux

To compress a single file, use the following command:

zip file.zip filename

This will compress the specified file in the current directory while keeping the original file intact.

4. Zip Multiple Files in Linux

For compressing multiple files into a single zip file, use the command given below:

zip zip_file file1 file2 file3

This will compress all the specified files into a single zip file.

5. Add Files to an Existing Zip File

Sometimes you might miss a file while compressing multiple files. You can add a single file to an existing zip file. This can be done using the -u flag, which stands for the update. To add a new file to an existing zip file, use the following command:

zip -u zip_file new_file

This command will add the new_file to the existing zip_file

6. Remove File from an Existing Zip File

While zipping multiple files, it may be possible that you may have added a sensitive file to the zip file. Thankfully, you can easily delete a file from an existing zip file by using the following command:

zip -d zip_file file_to_be_deleted

Here, the -d flag stands for delete which will delete the file file_to_be_deleted from the existing zip_file.

7. How to Zip a Directory in Linux

In some cases, you might want to zip the entire contents of a directory, but writing all the file names one-by-one in the command is not feasible. In such a case, you can use the -r flag to recursively or repeatedly add the files of a directory to the zip file.

zip -r zip_file directory_name

This will add all the files in directory_name to the zip_file

8. Create a password-protected zip file

Some sensitive files need to be password protected to prevent unintended users from accessing them. One way to protect the files is to convert the files to zip and encrypt them using -e (encrypt flag). To create password protected zip file in Linux, use the following command:

zip -e zip_file file_1 file_2

You will be asked to enter and verify a password for the compressed file.

Also, you can also add multiple flags while compressing using the below command:

zip -r -e zip_file directory_name

Here, we are encrypting all the directory contents into a zip file.

How to Unzip Files Using the Linux Command Line

Decompressing a zip file means extracting all the information stored inside a zip file using various commands. In Linux, the most common command to decompress a zip file is the unzip command.

The basic syntax to use the command is:

unzip <option> zip_file, where

  • <option> specifies various options to use during decompression
  • zip_file specifies the zip file to decompress

1. View Contents of a Zip File

It is always advisable to view the contents of a zip file before extracting it, so as to check for any malware waiting to harm your system. In Linux, you can view the contents of a zip file without extracting it, using the following command:

unzip -l zip_file

To view more information about the Zip File, use the -Z flag, as shown below:

unzip -Z zip_file

2. Decompress a Single Zip File

To decompress a single zip file in the same directory, use the following command:

unzip zip_file

3. Unzip a Single File to a Different Directory

If you want to unzip a zip file to a different directory, use the -d flag as shown below:

unzip zip_file -d path/to/directory

4. Unzip Multiple Files in Linux

Suppose there are multiple zip files and you want to extract them all. Use the modified version of unzip command to extract all the zip files as shown below:

unzip "*.zip

Understanding the command: Here, we are looking for all the files with the extension .zip which is specified by the wildcard "*” which is then passed to the unzip command one by one.

5. Decompress a Zip File Excluding Some Files

If, after viewing the contents of a zip file, you want to extract only some of the files. It can easily be done using the unzip command in Linux. To extract a zip file while excluding some files, use the -x flag to specify the filename you want to exclude as shown in the following command:

unzip zip_file –x file_name

Guide to Zip and Unzip Files & Directory in Linux

We all need to zip and unzip files irrespective of the operating system we are working on. Zip files can sometimes help to save a lot of precious memory by compressing the files. In this article, we have discussed two methods to zip and unzip files in Linux with examples. Using the command line method is faster, provides more options to work with, and is the preferred method for most Linux users. Do let us know in the comments if you face any issues with the zip and unzip commands

Frequently Asked Questions

What are zip and unzip commands in Linux?

The zip command is a popular command-line tool used to compress files in Linux. It has many options such as combining multiple files and directories into a single zip file, compressing files with a password, etc. The unzip command is used to decompress or extract the contents from a zip file.

What does it mean to zip and unzip files?

Zip is an archive file format that is used to compress one or more files together into a single zip file. It reduces file size without losing data which makes it easier to transport or store. The unzip tool is used to extract a zip file in the original format.

What is a zip file used for?

A zip file is an archive format containing multiple files combined and compressed into a single file. Being in compressed file format, a zip file occupies less space and consumes less bandwidth while transferring.

Comments 0
Leave a Reply