Zip Command on Linux Explained with Examples for Beginners

Zip Command on Linux Explained with Examples

In this tutorial we will explain how to zip files in Linux from command line using the zip command.

ZIP is the most widely used archive file format that supports 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, along with information about the files. In addition to, an entire directory structure can be packed into a zip archive with a single command.

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

Install zip command on Fedora / CentOS / RedHat

yum install zip

Install zip command on Ubuntu / Debian / Linux Mint

apt install zip

How to ZIP Files and Directories in Linux

To create a ZIP file using 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 a 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
  adding: file1.txt (deflated 86%)
  adding: file2.txt (deflated 49%)

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

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

zip -q my-archive.zip file1.txt file2.txt

To include sub-directories and everything contained in them in the ZIP file, use the -r (recursive) option and include the name of the sub-directory on the command line.

zip -r my-archive.zip directory1/ directory2/ file1.txt file2.txt 

Adding passwords to ZIP files using 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 to re-enter it for verification.

zip -e my-archive.zip file1.txt file2.txt
Enter password: 
Verify password: 
  adding: file1.txt (deflated 86%)
  adding: file2.txt (deflated 49%)

You can add or update one or more files of the compressed archive. Suppose we have compressed an archive then, modified a file. There is a possibility to add the update file to the compressed archive with the -u (update) option.

zip -u my-archive.zip file1.txt file3.txt
updating: file1.txt (deflated 92%)
  adding: file3.txt (deflated 61%)

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.

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

Leave a Reply

Your email address will not be published.