Mastering Zip and Unzip Commands on Linux

Linux TLDR
Last Updated:
Reading time: 4 minutes

In Linux, β€œzip” and β€œunzip” are two popular command-line utilities that are used to create or extract Zip archives, respectively.

These utilities are capable of efficiently compressing and decompressing large files or directories, all while ensuring the preservation of their original file structures, permissions, and metadata.

This kickstart guide will teach you the distinctions between Zip and Tarball and the installation of Zip and Unzip on your chosen Linux system, with command examples.

Tutorial Details

DescriptionZip and Unzip
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Internet RequiredNo

Difference Between Zip and Tarball

When you begin your Linux journey, you will find out that there are two types of tools, Zip and Tarball (or tar), used to provide archive (or compressed) files.

While the common differences between them are quite predictable, they vary in terms of extensions, features, and command-line options.

What sets them apart most significantly is their compression algorithm. Zip utilizes the Zip compression algorithm, a lossless method designed to minimize file size while safeguarding data integrity.

However, Tarballs rely on external compression tools such as β€œgzip” or β€œxz” to generate compressed tar archives (e.g., β€œtar.gz” or β€œtar.xz” files), as they lack a native compression algorithm.

How to Install Zip and Unzip on Linux

Although widely available in Linux distributions, the β€œzip” and β€œunzip” utilities might be absent in rare cases. In such situations, select the suitable command for your distribution to install them.

Installing Zip and Unzip on a Debian or Ubuntu System

$ sudo apt install zip 
$ sudo apt install unzip

Installing Zip and Unzip on a Red Hat or Fedora System

$ sudo dnf install zip 
$ sudo dnf install unzip

Installing Zip and Unzip on an Arch or Manjaro System

$ sudo pacman -S zip 
$ sudo pacman -S unzip

Installing Zip and Unzip on an OpenSUSE-Based System

$ sudo zypper install zip 
$ sudo zypper install unzip

Verify the Installation

After completing the installation, you can ensure command accessibility via the terminal by executing the following command:

$ zip -v
$ unzip -v


Verify the zip and unzip installations

Installation is complete. Let’s now explore a few examples showcasing the usage of the β€œzip” and β€œunzip” commands.

How to Use Zip and Unzip Commands on Linux

Please note that while there are numerous options available for the β€œzip” and β€œunzip” commands, I’ve focused on including only the most common and essential ones in this guide, making it easier for beginners to grasp. Let’s begin with…

Usage of the Zip Command

Here are some practical examples of how to use the β€œzip” command:

Create a Zip Archive

To create a Zip archive, simply provide the desired name for the archive file as the initial parameter, followed by a list of the files and directories you wish to include within the archive.

The command provided excludes files and subdirectories within β€œdirectory1/β€œ.
$ zip file1.txt file2.txt directory1/


Compressing files and directories

Including Subdirectories

Traditionally, the β€œzip” command excludes files and subdirectories within the specified directory.

However, by using the β€œ-r” flag, you can recursively include both file and subdirectory content in the archive.

$ zip -r file1.txt file2.txt directory1/


Recursively compressing files and directories

Excluding Files/Directories

If the directory you intend to archive contains specific files (such as β€œ.log” and β€œ.tmpβ€œ) that you do not want to include in your archive.

You can exclude these specific files or directories from the archive using the β€œ-x” flag.

$ zip -r directory1/ -x "*.log" "*.tmp" "directory1/temp/*"


Excluding certain files and directories while compressing

Verbose Output

To see a detailed list of files and directories that are being added to the archive, use the β€œ-v” flag.

$ zip -rv directory1/


Verbose output while compressing files

Encrypting the Archive

You can password-protect the zip archive using the β€œ-e” flag; it will prompt you to enter a password before the archive is created.

$ zip -er file1.txt file2.txt directory1/


Setting the password for the archive

Updating an Existing Archive

If you want to add new files or directories to an existing archive, you can use the β€œ-u” flag.

$ zip -u newfile.txt


Updating the existing archive

Viewing Archive Contents

To list the contents of an archive without extracting it, use the β€œ-sf” flag with the β€œzip” command.

$ zip -sf


$ unzip -l


Looking into the archive content

Usage of the Unzip Command

Here are some practical examples of how to use the β€œunzip” command:

Basic Extraction

Use the following command to extract all files and directories from a Zip archive named β€œmyarchive.zipβ€œ.

$ unzip


Extracting the Zip archive

Extract to a Specific Directory

Using the β€œ-d” flag, you can specify the directory where the extracted files will be placed.

$ unzip -d ~/Documents/


Extracting the archive content to a specific directory

Extract Specific Files

To extract particular files or directories from the archive, ensure you specify them simultaneously.

To extract files that match a specific pattern, such as β€œ.txt” files, you can employ wildcards. Utilize β€œ*.txt” to exclusively extract files with this extension.
$ unzip file1.txt file2.txt


Extracting specific files from the archive

Excluding Files

You can exclude certain files or directories from being extracted using the β€œ-x” flag.

$ unzip -x file2.txt directory1/*


Excluding certain files and directories from being extracted

Overwrite Existing Files

Unzip’s default behavior avoids overwriting files or directories at the target location if they already exist, but you can enforce overwriting by using the β€œ-o” flag.

$ unzip -o


Overwriting the existing files and directories

Quiet Mode

To suppress all output except for error messages, you can use the β€œ-q” flag:

$ unzip -q


Silently extracting the content of archive

That’s it for now.

Final Word

The process of compressing and decompressing files and directories can be a tedious task, particularly within the Linux environment, where an array of tools exist for achieving the same task.

Yet, I tried to make this article simple and easier for beginners to grasp. If you have any questions or queries related to this article, then feel free to ask them in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.