Author : Piyush Gupta
The zip command is used for compression and file packaging under Linux/Unix operating systems. unzip is used for decompress an archive. See the below examples of some typical uses of zip and unzip.
1 – Zip All Files in Directory
This command will create zip of all files in /backup directory. I will not create zip recursively.
$ zip backup.zip /backup/*
Sample Output:
adding: backup/anaconda.ifcfg.log (deflated 47%)
adding: backup/anaconda.log (deflated 78%)
adding: backup/anaconda.program.log (deflated 84%)
adding: backup/anaconda.storage.log (deflated 90%)
adding: backup/boot.log (deflated 72%)
adding: backup/dracut.log (deflated 92%)
adding: backup/httpd/ (stored 0%)
adding: backup/kadmind.log (deflated 74%)
adding: backup/krb5kdc.log (deflated 71%)
adding: backup/mysqld.log (deflated 82%)
2 – Zip files with Wildcard
Use Linux wildcards to archive files of specific extensions only. Like backup only .log extension files in a directory.
$ zip backup.zip /backup/*.log
Sample Output:
adding: backup/anaconda.ifcfg.log (deflated 47%)
adding: backup/anaconda.log (deflated 78%)
adding: backup/anaconda.program.log (deflated 84%)
adding: backup/anaconda.storage.log (deflated 90%)
adding: backup/boot.log (deflated 72%)
adding: backup/dracut.log (deflated 92%)
adding: backup/kadmind.log (deflated 74%)
adding: backup/krb5kdc.log (deflated 71%)
adding: backup/mysqld.log (deflated 82%)
adding: backup/pm-powersave.log (deflated 15%)
adding: backup/wpa_supplicant.log (stored 0%)
adding: backup/Xorg.0.log (deflated 83%)
adding: backup/Xorg.9.log (deflated 83%)
adding: backup/yum.log (deflated 77%)
3 – Zip files Recursively
Below command will create an archive recursively with files in sub directories also.
$ zip -r backup.zip /backup
4 – Create Password Protected Zip
Some times we need to create password protected archive. Use -p to make an archive password protected.
$ zip -P backup.zip /backup/*.log
5 – Zip with Compression Levels
Zip command provides 10 levels of compression ( 0-9 ).
- -6 is used as default compression level.
- -0 is used for lowest level compression.
- -9 is used for hightest level comression
$ zip -9 high-compressed-file.zip /backup/*
$ zip -0 lowest-compressed-file.zip /backup/*
Check differences between compressed file
$ ls -lh lowest-compressed-file.zip high-compressed-file.zip
-rw-r--r--. 1 root root 50K Apr 11 14:14 high-compressed-file.zip
-rw-r--r--. 1 root root 447K Apr 11 14:14 lowest-compressed-file.zip
You can see the difference between between both file sizes.
6 – List content of zip File
Using -l switch with unzip command to list only files inside a zip archive without decompressing it.
$ unzip -l backup.zip
Sample Output:
Archive: backup.zip
Length Date Time Name
--------- ---------- ----- ----
140 04-11-2013 14:07 backup/anaconda.ifcfg.log
11153 04-11-2013 14:07 backup/anaconda.log
15446 04-11-2013 14:07 backup/anaconda.program.log
136167 04-11-2013 14:07 backup/anaconda.storage.log
2722 04-11-2013 14:07 backup/boot.log
211614 04-11-2013 14:07 backup/dracut.log
0 04-11-2013 14:08 backup/httpd/
1382 04-11-2013 14:07 backup/kadmind.log
1248 04-11-2013 14:07 backup/krb5kdc.log
6485 04-11-2013 14:07 backup/mysqld.log
87 04-11-2013 14:07 backup/pm-powersave.log
0 04-11-2013 14:07 backup/wpa_supplicant.log
30186 04-11-2013 14:07 backup/Xorg.0.log
31094 04-11-2013 14:07 backup/Xorg.9.log
6739 04-11-2013 14:07 backup/yum.log
--------- -------
454463 15 files
7 – Extract a Zip File.
unzip command is used to extract a zip file. Use below command to simply extract a zip file.
$ unzip backup.zip
8 – Check an archive file
Use -t to check and archive files. This option extracts each specified file in memory and compares the CRC (cyclic redundancy check, an enhanced checksum) .
$ unzip -t backup.zip
Sample Output:
Archive: backup-11Apr2013.zip
testing: backup/anaconda.ifcfg.log OK
testing: backup/anaconda.log OK
testing: backup/anaconda.program.log OK
testing: backup/anaconda.storage.log OK
testing: backup/boot.log OK
testing: backup/dracut.log OK
testing: backup/httpd/ OK
testing: backup/kadmind.log OK
testing: backup/krb5kdc.log OK
testing: backup/mysqld.log OK
testing: backup/pm-powersave.log OK
testing: backup/wpa_supplicant.log OK
testing: backup/Xorg.0.log OK
testing: backup/Xorg.9.log OK
testing: backup/yum.log OK
No errors detected in compressed data of backup.zip.
No comments:
Post a Comment