Zip is a compress tool which is available in most of the operating systems such as Linux/Unix, Apple OS, Microsoft OS etc. In this post we will see how to install, use and tips about zip command.

How to install zip and unzip command in Linux?

On Redhat, Centos and Fedora based machines

yum install zip

yum install unzip

On Ubuntu and Debian based machines

apt-get install zip

apt-get install unzip

We will learn below requirements with zip command examples

Zipping individual files
Zip a folder
Zip a folder and it’s all content
Zip a folder to a different location

Below are some zip commands which are frequently used and we will learn them with some good examples

Example1: Syntax for zipping a file or folder.
Syntax:

zip archivename.zip file1 file2 folder1

Note: The extension .zip is not mandatory and this is useful only to identify the file zip file.

Example2: Zip individual files to a zip archive

zip abc.zip file1 file2 file3

Output:

[email protected]:~/test/1$ ls -l file*
-rw-rw-r-- 1 surendra surendra 188 May 8 10:12 file1
-rw-rw-r-- 1 surendra surendra 48894 May 8 10:12 file2
-rw-rw-r-- 1 surendra surendra 41 May 8 10:12 file3
[email protected]:~/test/1$ zip abc.zip file1 file2 file3
adding: file1 (deflated 40%)
adding: file2 (deflated 54%)
adding: file3 (deflated 12%)
[email protected]:~/test/1$ du -hs abc.zip
24K abc.zip

Example 3: Zipping a folder is a tricky thing as by default zip will not zip entire folder content such as sub folders and files let us see how a zip command work by default on a folder

zip abc.zip 1/

Output:

[email protected]:~/test$ ls
1
[email protected]:~/test$ zip abc.zip 1/
adding: 1/ (stored 0%)
[email protected]:~/test$ ls -l
total 8
drwxrwxr-x 5 surendra surendra 4096 May 8 10:12 1
-rw-rw-r-- 1 surendra surendra 154 May 8 10:15 abc.zip

If you observe the folder is zipped and 0% is stored which means it did not zip entire folder. To zip first level of folder content use * as shown below

Output:
[email protected]:~/test$ rm abc.zip
[email protected]:~/test$ zip abc.zip 1/*
adding: 1/2/ (stored 0%)
adding: 1/3/ (stored 0%)
adding: 1/4/ (stored 0%)
adding: 1/abc.zip (stored 0%)
adding: 1/bash-support.zip (stored 0%)
adding: 1/file1 (deflated 40%)
adding: 1/file2 (deflated 54%)
adding: 1/file3 (deflated 12%)

Actually there are sub folders and files in 1 folder, in order to zip all content of a folder use -r option

zip -r abc.zip 1/

Output:

[email protected]:~/test$ du -hs abc.zip
188K abc.zip
[email protected]:~/test$ rm abc.zip
[email protected]:~/test$ zip -r abc.zip 1/
adding: 1/ (stored 0%)
adding: 1/bash-support.zip (stored 0%)
adding: 1/file2 (deflated 54%)
adding: 1/2/ (stored 0%)
adding: 1/2/5/ (stored 0%)
adding: 1/2/5/test.sh (deflated 57%)
adding: 1/2/6/ (stored 0%)
adding: 1/2/6/dump2.doc (deflated 63%)
adding: 1/3/ (stored 0%)
adding: 1/3/5/ (stored 0%)
adding: 1/3/5/abc.txt (deflated 40%)
adding: 1/3/5/dump2.doc (deflated 63%)
adding: 1/file3 (deflated 12%)
adding: 1/abc.zip (stored 0%)
adding: 1/4/ (stored 0%)
adding: 1/4/7/ (stored 0%)
adding: 1/4/7/dump1.doc (deflated 63%)
adding: 1/4/7/dump.doc (deflated 63%)
adding: 1/4/6/ (stored 0%)
adding: 1/4/6/dump.doc (deflated 63%)
adding: 1/file1 (deflated 40%)
[email protected]:~/test$ du -hs abc.zip
1.3M abc.zip

If you observe the size of abc.zip is increased considerably and the out of the commend will give you what files are zipped.

Example 4: How to zip files which are not located in present directory? Suppose I want to zip /home/surendra/test/1 folder in /tmp and I am at /var folder?

cd /var
zip -r /tmp/abc.zip /home/surendra/test/1
du -hs /tmp/abc.zip

If you observe we taken zip in /tmp directory.

Some times we want to take backups in a tape archive. To directly zip a folder on to tape archive use below command

zip -r - . | dd of=/dev/nrst1 obs=4k

where nrst1 is my tape archive
Example5: To compress fast use -1 option and for compress better ratios use -9

zip -1 -r abc.zip 1/

Clipped output for below time command

[email protected]:~/test$ time zip -1 -r abc.zip 1/
updating: 1/ (stored 0%)
.....
updating: 1/file1 (deflated 37%)
real 0m0.125s
user 0m0.043s
sys 0m0.003s
[email protected]:~/test$ time zip -9 -r abc.zip 1/
updating: 1/ (stored 0%)
.....
updating: 1/file1 (deflated 40%)
real 0m0.150s
user 0m0.147s
sys 0m0.003s

If you observe the time taken for zip -9 is slightly greater than zip -1 command

Ok, now we are done with basic zipping files let see how to list, update, delete files in a zip file.

Example6: How to exclude a file in a folder when compressing it.

zip -r abc.zip 1/ -x 1/bash-support.zip

Note: When you want to exclude a file/folder use -x option at the end of zip command as show above

Output:
[email protected]:~/test$ zip -r abc.zip 1/ -x 1/bash-support.zip
adding: 1/ (stored 0%)
adding: 1/file2 (deflated 54%)
adding: 1/2/ (stored 0%)
adding: 1/2/5/ (stored 0%)
adding: 1/2/5/test.sh (deflated 57%)
adding: 1/2/6/ (stored 0%)
adding: 1/2/6/dump2.doc (deflated 63%)
adding: 1/3/ (stored 0%)
adding: 1/3/5/ (stored 0%)
adding: 1/3/5/abc.txt (deflated 40%)
adding: 1/3/5/dump2.doc (deflated 63%)
adding: 1/file3 (deflated 12%)
adding: 1/abc.zip (stored 0%)
adding: 1/4/ (stored 0%)
adding: 1/4/7/ (stored 0%)
adding: 1/4/7/dump1.doc (stored 0%)
adding: 1/4/7/dump.doc (deflated 63%)
adding: 1/4/6/ (stored 0%)
adding: 1/4/6/dump.doc (stored 0%)
adding: 1/file1 (deflated 40%)
[email protected]:~/test$

Eample7: List all the files stored in a zip file

unzip -l abc.zip

or
less abc.zip

or

zipinfo -1 abc.zip

Output:

[email protected]:~/test$ unzip -l abc.zip
Archive: abc.zip
Length Date Time Name
--------- ---------- ----- ----
0 2014-05-08 10:12 1/
143449 2014-05-08 10:07 1/bash-support.zip
48894 2014-05-08 10:12 1/file2
0 2014-05-08 10:09 1/2/
0 2014-05-08 10:07 1/2/5/
543 2014-05-08 10:07 1/2/5/test.sh
0 2014-05-08 10:08 1/2/6/
588895 2014-05-08 10:08 1/2/6/dump2.doc
0 2014-05-08 10:09 1/3/
0 2014-05-08 10:08 1/3/5/
188 2014-05-08 10:07 1/3/5/abc.txt
88895 2014-05-08 10:08 1/3/5/dump2.doc
41 2014-05-08 10:12 1/file3
23186 2014-05-08 10:12 1/abc.zip
0 2014-05-08 10:09 1/4/
0 2014-05-08 10:08 1/4/7/
588895 2014-05-08 10:08 1/4/7/dump1.doc
588895 2014-05-08 10:08 1/4/7/dump.doc
0 2014-05-08 10:08 1/4/6/
588895 2014-05-08 10:08 1/4/6/dump.doc
188 2014-05-08 10:12 1/file1
--------- -------
3160964 21 files

Example 8: Delete a file in an archive without extracting entire zip file.

zip -d abc.zip path/to/file

Output:

[email protected]:~/test$ zip -d abc.zip 1/bash-support.zip
deleting: 1/bash-support.zip
[email protected]:~/test$ unzip -l abc.zip
Archive: abc.zip
Length Date Time Name
--------- ---------- ----- ----
0 2014-05-08 10:12 1/
48894 2014-05-08 10:12 1/file2
0 2014-05-08 10:09 1/2/
0 2014-05-08 10:07 1/2/5/
543 2014-05-08 10:07 1/2/5/test.sh
0 2014-05-08 10:08 1/2/6/
588895 2014-05-08 10:08 1/2/6/dump2.doc
0 2014-05-08 10:09 1/3/
0 2014-05-08 10:08 1/3/5/
188 2014-05-08 10:07 1/3/5/abc.txt
588895 2014-05-08 10:08 1/3/5/dump2.doc
41 2014-05-08 10:12 1/file3
23186 2014-05-08 10:12 1/abc.zip
0 2014-05-08 10:09 1/4/
0 2014-05-08 10:08 1/4/7/
588895 2014-05-08 10:08 1/4/7/dump1.doc
588895 2014-05-08 10:08 1/4/7/dump.doc
0 2014-05-08 10:08 1/4/6/
588895 2014-05-08 10:08 1/4/6/dump.doc
188 2014-05-08 10:12 1/file1
--------- -------
3017515 20 files

If you observe there is no bash-support.zip file in my zip file

Example9: To update a particular file which is modified and we want to update our zip file with this update use -u option

zip -u abc.zip 1/2/6/dump2.doc

output(Clipped):

[email protected]:~/test$ unzip -l abc.zip
Archive: abc.zip
Length Date Time Name
--------- ---------- ----- ----
0 2014-05-08 10:12 1/

0 2014-05-08 10:08 1/2/6/
588895 2014-05-08 10:08 1/2/6/dump2.doc

188 2014-05-08 10:12 1/file1
--------- -------
3017515 20 files
[email protected]:~/test$ vi 1/2/6/dump2.doc
[email protected]:~/test$ zip -u abc.zip 1/2/6/dump2.doc
updating: 1/2/6/dump2.doc (deflated 63%)
[email protected]:~/test$ unzip -l abc.zip
Archive: abc.zip
Length Date Time Name
--------- ---------- ----- ----
0 2014-05-08 10:12 1/

0 2014-05-08 10:08 1/2/6/
588912 2014-05-08 11:03 1/2/6/dump2.doc

188 2014-05-08 10:12 1/file1
--------- -------
3017532 20 files

Example10: Update all the files in zip file if the original files are modified

zip -fr abc.zip 1/
Output:
[email protected]:~/test$ > 1/4/6/dump.doc
[email protected]:~/test$ > 1/4/7/dump1.doc
[email protected]:~/test$ zip -fr abc.zip 1/
freshening: 1/2/6/ (stored 0%)
freshening: 1/4/7/dump1.doc (stored 0%)
freshening: 1/4/6/dump.doc (stored 0%)

if you observe I emptied dump.doc and dump1.doc using “>” in 1/4/6 and 1/4/7 folders respectively and that is the reason only these files are updated to my zip file.

Now comes the extracting..

Example 11: Extract your files from a zip folder

unzip abc.zip

Example12: To extract to a specific directory use -d option

unzip abc.zip -d /tmp

Output:

[email protected]:~/test$ unzip abc.zip -d test/
Archive: abc.zip
creating: test/1/
inflating: test/1/file2
creating: test/1/2/
creating: test/1/2/5/
inflating: test/1/2/5/test.sh
creating: test/1/2/6/
inflating: test/1/2/6/dump2.doc
creating: test/1/3/
creating: test/1/3/5/
inflating: test/1/3/5/abc.txt
inflating: test/1/3/5/dump2.doc
inflating: test/1/file3
extracting: test/1/abc.zip
creating: test/1/4/
creating: test/1/4/7/
extracting: test/1/4/7/dump1.doc
inflating: test/1/4/7/dump.doc
creating: test/1/4/6/
extracting: test/1/4/6/dump.doc
inflating: test/1/file1
[email protected]:~/test$ cd test/
[email protected]:~/test/test$ ls
1

Example13: Extract specific file from an archive

unzip abc.zip 1/2/5/test.sh

In our next post we will see other zipping software’s available.