How to Zip Files and Directories on Linux

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> ...

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.txt
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.txt
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.txt
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.txt
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 is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, Bobby has worked as a Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Leave a Reply

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