How to zip folder on Linux

Zip a folder in Linux

In this post, we will learn how to zip a folder in Linux with examples. Sometimes we want to save space in our computer/server or organize the files, protect them, or send them to another person or computer.

Like the tar.gz format, the ZIP goal is to compress and file the packaging command line for Unix. Each file is collected in a individual .zip {.zip-filename} file with the extension .zip. And it’s available in all Unix/Linux versions also for macOS and Windows.

Today there are several compression methods available. Zip is one of the most traditional ones.

Since 1989 (created by Philip Katz), System Administrators uses the Zip method to reduce files and directories on hard-drive and save space.

So, this post will see how you can quickly zip folders and directories on Linux using the zip command.

Requirement: Check if you have the zip is installed

Typically zip is available. You can execute the below command to install zip. If it’s your computer doesn’t have it yet.

For Ubuntu, Mint, or any other Debian based:

sudo apt-get install -y zip unzip


For CentOS, Fedora:

sudo yum install -y zip unzip

So, we can continue now and, you will learn how to zip a folder in Linux.

How to zip a directory in Linux

To use the zip command, for example, we need follow this sequence:

First, give the file name (the ZIP file itself). Second, specify the files or folders that you would like to add to the archive separated by space, following the example below:

zip -r <zip-files-name>.zip <file-name-a> <file-name-a> <file-name-a> <folder-name-a> <folder-name-b>
adding: file-name-a (deflated 12%)
adding: file-name-b (stored 40%)

The -r option means recurse. It’s necessary when we need to compress folders and their contents too. In other words, guarantee that the zip archive of a directory will also include the content of subdirectories. 

When you execute the zip command, the default output contains the names of the files and folders added to the archive. If you would like to remove the standard output, you can use the -q (quiet mode) option to suppress it.

All zip files created automatically end with .zip (file extension) even if you don’t specify on the command line.

How to define the Level of Compression

First, you need to know that several compression methods exist, and each one has your efficiency to compress a file. Second, is not all files could be compressed. In this case, the file size is not reduced, It only archives it inside the zip file.

For example, when you run the zip command, the default compression method is the deflate.

Similarly, there is another algorithm called Burrows-Wheeler, another free and open-source program used by bzip2.

How to use another compression method?

To change a compression method, add the -Z option with the compress method name.

zip -r -Z bzip2 folder-name

How to specify the Compression Level?

First, you may be thinking, why would I choose the compression level if, in an ideal world, we would like to save much space that we could? Also, our goal to use Zip or any other compress method is to decrease the amount of data. 

However, let’s imagine a scenario that you need to back up a considerable database folder from one server with at least 100GB. In this case, you don’t want to wait hours to get the backup done. In addition, in scenarios like this one, sometimes we are not worried about space. So, to speed up, we can define the compression level to 0. Doing it will be faster because it will no require CPU utilization to compress the files.

Also, keep in mind higher the compression level, the more CPU cost, so more it will take to compress.

The zip command admits a compression level between 0 and 9. When you don’t specify the compression level, the default is -6.

So, to specify the compression level, you need to add dash followed by compression level number. Like the example below:

zip -9 -r /home/bitslovers/Pictures

In this example, we will use maximum compression. However, if you don’t want to compress, you need to replace it with -0.

How to zip files in Linux, only for a collection of files

Imagine a real scenario where we have a folder on your computer containing several types of files, and you would like to zip only the image files with extension png. So, to filter a specific kind of file, we can combine with find command and xargs with pipe to give the zip command that parameters (the list of files).

find . -name "*.png" | xargs zip -r

How to update a file inside of a Zip file?

Sometimes it’s easier to update a specific file with a single command line instead of unzipping and zip again to edit one file.

For example, let’s suppose that you have a backup from all your shell scripts, and later, you changed one script that already exists inside your zip file. To update this script inside of the zip, we can do like this example below:

zip -uq

How to Create a Zip file with Password in Linux

To protect your files inside the zip file, it’s easy and straightforward. We need to add the -e option on the zip file command like the example below:

zip -e *

When you execute the command above, it will prompt for a password and then confirm the password. So, later to unzip the files, the password will be prompt.


You have learned how to zip a folder in Linux, using the zip command to compress files and folder on Linux, that acknowledge is a must-have for who wants to start learning Linux.

Also, keep in mind the zip command file is not the unique alternative for this proposal. For Unix and Linux we will see a lot of tar.gz extensions on the internet. And we will cover that subject soon.

Leave a Comment

Your email address will not be published. Required fields are marked *

Free PDF with a useful Mind Map that illustrates everything you should know about AWS VPC in a single view.