Categories: Linux

Zip Command in Linux

Zip command is used to compress the files to reduce file size and also used as file package utility. zip is available in many operating systems like unix, linux, windows etc.

If you have a limited bandwidth between two servers and want to transfer the files faster, then zip the files and transfer.

The syntax of zip command is

zip [options] zipfile files_list


The options of zip command are:

-d : Removes the file from the zip archive

-u : Updates the file in the zip archive

-m : Deletes the original files after zipping.

-r : Recursively zips the files in a directory

-x : Exclude the files in creating the zip

-v : verbose mode

-1 : Compresses the files faster

-9 : Compresses the files better

-f : freshen only changed files.

zipfile : creates the zip file with name as zipfile.zip

files_list : list of files to be zipped.


Zip Command Examples:

The files in my current directory are listed below:

documents/1.pdf

documents/2.pdf

documents/3.pdf

file1.txt

file2.txt


Here documents is a directory which contains the files 1.pdf, 2.pdf and 3.pdf. We will see how to use zip command with examples.

1. Creating a zip file

The zip command in linux creates an archive with the specified files. This is shown below:

> zip archive file1.txt file2.txt

adding: file1.txt (deflated 80%)

adding: file2.txt (deflated 80%)

> ls

archive.zip documents file1.txt file2.txt


The above command creates the zip file with name archive.zip

2. Extracting files from zip

To extract files from the zip, use the unzip command in linux. This is shown below:

> unzip archive.zip

Archive: archive.zip

inflating: file1.txt

inflating: file2.txt

> ls

archive.zip file1.txt file2.txt


3. Removing file from a zip file

After creating a zip file, you can remove a file from the archive using the -d option. To remove the file file2.txt from the archive, run the below zip command:

> zip -d archive.zip file2.txt

deleting: file2.txt

> unzip archive.zip

Archive: archive.zip

inflating: file1.txt


4. Update existing zip file

You can update the files in already created zip file. If any of the files are modified after zipping, you can fresh the zip file with only those modified files using the -f option.

> zip -f archive.zip

freshening: file1.txt (stored 0%)


Another way is using the -u option. This option can be used to update the specified list of files or add new files to the existing zip file.

> zip -u archive.zip file1.txt temp

updating: file1.txt (deflated 79%)

adding: temp (stored 0%)


5. Recursively zip files in directory.

To zip a directory recursively, use the -r option with the zip command. This example is shown below:

> zip -r dir_archive documents

adding: documents/ (stored 0%)

adding: documents/1.pdf (stored 0%)

adding: documents/2.pdf (stored 0%)

adding: documents/3.pdf (stored 0%)


6. Excluding files

Let say you are zipping all the files in the current directory and want to exclude some unwanted files. You can exclude these unwanted files using the -x option.

zip exclude_archive * -x file1.txt


The above command zips all the files in the current directory except the file file1.txt

7. Faster compressing

You can compress the files very fast using the -1 option with zip command. An example is shown below with and without using fast compression.

> zip -1 fast_archive file1.txt

adding: file1.txt (deflated 79%)

>zip normal_archive file1.txt

adding: file1.txt (deflated 80%)


If you use fast compression, the archive file created will occupy more space (size) when compared to normal compression.

8. Better compression.

To reduce more amount of size the files occupied, you can use the -9 option with the zip command. This gives a better compression.

> zip -9 better_archive file1.txt

adding: file1.txt (deflated 81%)


Compare the deflated percentages in the example 7 and 8.

Nitesh Shah

Share
Published by
Nitesh Shah
Tags: Linuxzip

Recent Posts

How to setup first Azure Virtual Machine?

Setting up your first Azure Virtual Machine can be done by following these steps: Create…

1 year ago

How to setup Amazon Cloudfront and S3 to serve static resources

Amazon CloudFront is a content delivery network (CDN) that helps you serve static content such…

1 year ago

Step-By-Step Guide To Setting Up An AWS Application Load Balancer

Step-By-Step Guide To Setting Up An AWS Application Load Balancer Are you looking for a…

1 year ago

How to restore MySQL database from .frm and .ibd files?

MySQL databases often get corrupted due to issues like hardware failure, file system failure etc.…

4 years ago

SQL Server Replication

SQL Server Replication is the process of copying databases from one node to another to…

5 years ago

101 System Admin Tools to make life easy

Here are 101 System Admin tools which make System Admins' life easy.

7 years ago

This website uses cookies.