How to install zip and unzip command in Linux?

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:

surendra@linuxnix:~/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
surendra@linuxnix:~/test/1$ zip abc.zip file1 file2 file3
adding: file1 (deflated 40%)
adding: file2 (deflated 54%)
adding: file3 (deflated 12%)
surendra@linuxnix:~/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:

surendra@linuxnix:~/test$ ls
1
surendra@linuxnix:~/test$ zip abc.zip 1/
adding: 1/ (stored 0%)
surendra@linuxnix:~/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:
surendra@linuxnix:~/test$ rm abc.zip
surendra@linuxnix:~/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:

surendra@linuxnix:~/test$ du -hs abc.zip
188K abc.zip
surendra@linuxnix:~/test$ rm abc.zip
surendra@linuxnix:~/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%)
surendra@linuxnix:~/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

surendra@linuxnix:~/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
surendra@linuxnix:~/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:
surendra@linuxnix:~/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%)
surendra@linuxnix:~/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:

surendra@linuxnix:~/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 with out extracting entire zip file.

zip -d abc.zip path/to/file

Output:

surendra@linuxnix:~/test$ zip -d abc.zip 1/bash-support.zip
deleting: 1/bash-support.zip
surendra@linuxnix:~/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):

surendra@linuxnix:~/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
surendra@linuxnix:~/test$ vi 1/2/6/dump2.doc
surendra@linuxnix:~/test$ zip -u abc.zip 1/2/6/dump2.doc
updating: 1/2/6/dump2.doc (deflated 63%)
surendra@linuxnix:~/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:
surendra@linuxnix:~/test$ > 1/4/6/dump.doc
surendra@linuxnix:~/test$ > 1/4/7/dump1.doc
surendra@linuxnix:~/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:

surendra@linuxnix:~/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
surendra@linuxnix:~/test$ cd test/
surendra@linuxnix:~/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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值