How to Zip Files and Directories on Linux (with Examples)

This article will show you how to easily use the Zip command on Linux to Zip files and directories.

Zip is the most popular archive file format that allows for lossless data compression. It is used to compress the files to reduce file size.

The Zip program puts one or more compressed files into a single zip archive and information about the files. In addition, an entire directory structure can be packed into a Zip archive with a single command.

First, you must install the zip command because it is not installed by default in most Linux distributions.

Install zip command on Fedora / RedHat / AlmaLinux / Rocky Linux

sudo dnf install zip

Install zip command on Ubuntu / Debian / Linux Mint

sudo apt install zip

How to Zip Files and Directories in Linux

To create a Zip file using the zip command on Linux, you need to tell zip the name of the archive file and which files to include in it.

zip <archivename> <filename1> <filename2> ...Code language: HTML, XML (xml)

For example, to zip files named file1.txt and file2.txt to a zip file named my-archive.zip, the command would be:

zip my-archive.zip file1.txt file2.txtCode language: CSS (css)
Zip files in Linux

As a result, each file is listed as it’s added. In addition, the name of the file and the amount of compression achieved on that file are also shown.

Furthermore, if you do not want to see the output from the zip as the file is created, use the -q (--quiet) option.

zip -q my-archive.zip file1.txt file2.txtCode language: CSS (css)
Zip files in quite mode in Linux

To include sub-directories and everything contained in them in the Zip file, use the -r (--recurse-paths) option and have the subdirectories’ names on the command line.

zip -r my-archive.zip directory1/ directory2/ file1.txt file2.txt
Zip files and directories recursively in Linux

How to Create a Password Protected Zip file in Linux

Adding passwords to Zip files using the zip command in Linux is easy.

For example, you can use the -e (--encrypt) option, and youโ€™ll be prompted to enter your password and re-enter it for verification. Note that the password will not be displayed in the terminal as you enter it.

zip -e my-archive.zip file1.txt file2.txtCode language: CSS (css)
Adding passwords to ZIP files in Linux

How to Update Existing Zip File in Linux

For example, suppose we have compressed an archive and then modified a file. There is a possibility to add the updated files to the compressed archive with the -u (--update) option.

So, we will add the updated file1.txt and the newly created file3.txt to the archive.

zip -u my-archive.zip file1.txt file3.txtCode language: CSS (css)
Adding files to Zip archive in Linux

Conclusion

Now you know how to use the zip command on Linux. For detailed information, you can head to the command’s man page. To extract a Zip archive on a Linux system, you can use the unzip command.

Related: 20 Basic Linux Commands for Beginners Explained with Examples

Feel free to leave a comment if you have any questions.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%