Zip command in Linux is used for compressing files & folders to reduce their size for the purpose of archival, backup, or for file transfer. Zip command can compress single or multiple files and folders into a single compressed file with extension .zip. We can also compress the whole operations system/file structure into a compressed file.

Recommended Read: Essential Linux Commands You Should Know

Also Read: Introduction to Bash Scripting Tutorial

Let’s first discuss how we can install ZIP utility on our systems.


Installation

To install zip on your system, use the following commands,

Fedora

# dnf install zip

CentOS/RHEL

# yum install zip

Ubuntu/Mint

$ sudo apt-get install zip


Syntax for using zip command

# zip OPTIONS ARCHIVE.zip FILE1 FILE2 FOLDER1 FOLDER2

Now let’s discuss some examples for using zip command.


Examples of zip command

 

Create a zip file

To create a compressed zip file for files or folders, use the following command,

# zip compressed.zip test.txt test2.txt *.html


Zip a directory

To create zip file for a complete directory with all the files and folders inside, we can use the following command,

# zip -r compressed.zip test_folder


Create a password-protected zip file

To create a zip file that is protected with a password, we can use the following command,

# zip -e compressed.zip test.txt test2.txt *.html

You will then be asked to enter a password to protect the zip file.


Add files to a zip file

To add new files or update existing files to a created zip file, we can use the option ‘u’,

# zip -u compressed.zip new_file.txt

If the file already exists on the zip, then the old file will be overwritten.


Delete a file from zip file

If we wish to remove a file from already created zip, then we can use the option ‘d’,

# zip -d compress.zip file1.txt


Delete original files after zip creation

We can also make zip command to delete the original files from which the zip file was created by using the option ‘m’,

# zip -m compressed.zip test.txt test2.txt *.html


Using compression level in zip

Zip provides compression level from 0 to 9 with 0 being no compression at all & 9 being the most compressed, default compression is 6. We can mention the compression level as follows,

# zip -8 compressed.zip test.txt test2.txt *.html


Splitting zip file based on size

By default, a single zip file is created but we can also create multiple zip files i.e. splitting zip files based on the size of zip file. For example, we can create zip file with a zip file being a maximum of 2 GB with the help of the following command,

# zip -s 2g compressed.zip test.txt test2.txt *.html


These were some examples of using zip in Linux. Please do let us know your suggestions, question or queries using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter

Linux TechLab is thankful for your continued support.