ZIP Command Basics
HOW-to zip multiple files
[root@linuxguideco backup]# zip linuxguide_file.zip linuxguide_file1 linuxguide_file2
updating: linuxguide_file1 (deflated 99%)
adding: linuxguide_file2 (deflated 99%)
[root@linuxguideco backup]#
HOW-to zip a directory and its files recursively
[root@linuxguideco /]# zip -r /backup/linuxguide.zip linuxguide
sample output
[root@linuxguideco backup]# ls -l
total 36976
-rw-r--r-- 1 root root 37816934 May 12 11:23 linuxguide.zip
HOW-to unzip a *.zip compressed file
[root@linuxguideco backup]# unzip linuxguide.zip
To see a output during unzip pass the -v option as shown below.
[root@linuxguideco backup]# unzip -v linuxguide.zip
sample output
[root@linuxguideco backup]# unzip -v linuxguide.zip
Archive: linuxguide.zip
Length Method Size Ratio Date Time CRC-32 Name
-------- ------ ------- ----- ---- ---- ------ ----
0 Stored 0 0% 05-12-13 11:17 00000000 linuxguide/
856 Defl:N 628 54% 02-23-12 18:34 de237f42 linuxguide/rmt
1498 Defl:N 685 54% 05-12-13 11:07 f93e84c3 linuxguide/updatesd
-skip-
118 Defl:N 102 14% 05-12-13 11:07 ad292c9a linuxguide/cups
354 Defl:N 201 43% 05-12-13 11:07 3a5bd10d linuxguide/tmpwatch
-------- ------- --- -------
190712477 37281378 81% 3037 files
HOW-to list a content of zip file with uncompressing it
[root@linuxguideco backup]# unzip -l linuxguide.zip
sample output
Archive: linuxguide.zip
Length Date Time Name
-------- ---- ---- ----
0 05-12-13 11:17 linuxguide/
223 05-12-13 11:07 linuxguide/nmptra
877 05-12-13 11:07 linuxguide/mon
-skip-
296 05-12-13 11:07 linuxguide/ntpd
418 05-12-13 11:07 linuxguide/akewha
078 08-29-12 16:28 linuxguide/wat
137 05-12-13 11:07 linuxguide/mlocat
-------- -------
190712477 3037 files
Zip command advanced compression
There are 10 levels of compression by zip command provided.
Level 0 (zero) is the lowest level where there is the archive file without compression.
Level 1 performs little compression. But it will be very fast.
Level 6 is the default level compression.
Level 9 is the maximum compression. It will be slower compared to the default level. In my opinion, if you compress a large file, you should always use level 9.
In the example below, I have level 0 (zero), level 1, default level 6 and level 9 compression to the same directory. See the compressed file size.
[root@linuxguideco /]# zip /backup/linuxguide_file.zip /linuxguide_file
updating: linuxguide_file (deflated 99%)
[root@linuxguideco /]# zip -0 /backup/linuxguide_file-0.zip /linuxguide_file
adding: linuxguide_file (stored 0%)
[root@linuxguideco /]# zip -1 /backup/linuxguide_file-1.zip /linuxguide_file
adding: linuxguide_file (deflated 97%)
[root@linuxguideco /]# zip -6 /backup/linuxguide_file-6.zip /linuxguide_file
adding: linuxguide_file (deflated 99%)
[root@linuxguideco /]# zip -9 /backup/linuxguide_file-9.zip /linuxguide_file
adding: linuxguide_file (deflated 99%)
[root@linuxguideco backup]# ls -lhtr
total 2.7M
-rw-r--r-- 1 root root 1.3M May 14 05:44 linuxguide_file
-rw-r--r-- 1 root root 1.3M May 14 05:39 linuxguide_file-0.zip
-rw-r--r-- 1 root root 41K May 14 05:40 linuxguide_file-1.zip
-rw-r--r-- 1 root root 13K May 14 05:40 linuxguide_file-6.zip
-rw-r--r-- 1 root root 12K May 14 05:41 linuxguide_file-9.zip
-rw-r--r-- 1 root root 13K May 14 05:42 linuxguide_file.zip
[root@linuxguideco backup]#
Validate a Zip Archive
Sometimes you may want to validate a zip archive without extracting. To test the validity of the zip file, pass the option -t as shown below.
[root@linuxguideco backup]# unzip -t linuxguide_file.zip
Archive: linuxguide_file.zip
testing: linuxguide_file OK
No errors detected in compressed data of linuxguide_file.zip.
HOW-to unzip a file
[root@linuxguideco backup]# unzip linuxguide_file-9.zip
Archive: linuxguide_file-9.zip
inflating: linuxguide_file
[root@linuxguideco backup]#
Zip files password protection
Pass the option -P to the zip command to assign a password to the zip file.
[root@linuxguideco backup]# zip -P mysecurepwd linuxguide_file-protected.zip linuxguide_file
adding: linuxguide_file (deflated 99%)
[root@linuxguideco backup]#
[root@linuxguideco backup]# unzip linuxguide_file-protected.zip
Archive: linuxguide_file-protected.zip
[linuxguide_file-protected.zip] linuxguide_file password:
inflating: linuxguide_file
[root@linuxguideco backup]#
The above option is good if you use the command in a shell-script for background work. On the other hand, if you perform the compression on the interactive command line, you do not want to be seen in the password history. So it will use the option -e as shown below to assign a password.
[root@linuxguideco backup]# zip -e linuxguide_file-protected.zip linuxguide_file
Enter password:
Verify password:
adding: linuxguide_file (deflated 99%)
[root@linuxguideco backup]#
When you unzip a password protected file, it will ask for the password, as shown below.
[root@linuxguideco backup]# unzip linuxguide_file-protected.zip
Archive: linuxguide_file-protected.zip
[linuxguide_file-protected.zip] linuxguide_file password:
inflating: linuxguide_file
[root@linuxguideco backup]#
Tar command (tape archive)
tar command is used to convert a group of files into an archive.
The following command created a single archive backup file /backup/abc.tar. This archive will contain all the files and subdirectories.
[root@linuxguideco backup]# tar cvf /backup/abc.tar /abc/
sample output
/abc/abcnss_compat
/abc/abcpamc
/abc/abccap
-skip-
/abc/iptables/abcip6
/abc/iptables/abci
/abc/iptables/policy
[root@linuxguideco backup]#
option t will display all the files from the tar archive.
[root@linuxguideco backup]# tar tvf /backup/abc.tar
drwxr-xr-x root/root 0 2013-04-01 19:35:45 abc/
-rwxr-xr-x root/root 36468 2013-01-08 18:33:16 abc/abcnss
-rwxr-xr-x root/root 9868 2013-01-09 10:30:33 abc/abcpamc
-rw-r--r-- root/root 390824 2009-03-25 06:52:17 abc/abcgobject
-skip-
-rwxr-xr-x root/root 5312 2012-10-30 21:07:16 abc/abcipt_ah
-rwxr-xr-x root/root 8432 2012-10-30 21:07:16 abc/abcip6
-rwxr-xr-x root/root 6932 2012-10-30 21:07:16 abc/abcipL
-rwxr-xr-x root/root 10420 2012-10-30 21:07:16 abc/abcip6
[root@linuxguideco backup]#
x option to extract the files from the tar archive as shown below. This will extract the contents of the current directory location where the command is run.
[root@linuxguideco backup]# tar xvf /backup/abc.tar
abc.tar will be extracted.
[root@linuxguideco backup]# du -hsc /abc.tar
127M /abc.tar
127M total
HOW-to gzip with tar
Add option z to the tar command when dealing with tar.gz compressed file.
[root@linuxguideco backup]# tar cvfz /backup/linuxg.tar.gz /linuxguide
linuxguide/abc.tar
[root@linuxguideco tmp]#
option x will extract all the files from the tar archive.
[root@linuxguideco backup]# tar xvfz /backup/linuxg.tar.gz
option t will display all the files from the tar archive.
[root@linuxguideco backup]# tar tvfz /backup/linuxg.tar.gz
[root@linuxguideco backup]# du -hsc linuxg.tar.gz
60M linuxg.tar.gz
60M total
Note: Using gzip is faster when compared to bzip2.
HOW-to use bzip2 with tar
Add option j to the tar command when dealing with tar.bz2 compressed file.
[root@linuxguideco backup]# tar cvfj /backup/linuxg.tar.bz2 /linuxguide
/tmp/linuxguide/abc.tar
[root@linuxguideco tmp]#
option x will extract all the files from the tar archive.
[root@linuxguideco backup]# tar xvfj /backup/linuxg.tar.bz2
option t will display all the files from the tar archive.
[root@linuxguideco backup]# tar tvfj /backup/linuxg.tar.bz2
Note: Using bzip2 gives higher level of compression when compared to gzip.
In the end, we will summarize this topic what we've learnt from it. Highlights of the article is mentioned below:
how to zip multiple files
zip a directory and its files recursively
unzip a *.zip compressed file
unzip a zip file with v option
list a content of zip file with l option
zip command with advanced compression
validate a zip archive with t option
zip files with password protection
tar (tape archive) command
how to use gzip and bzip2 with tar
We've prepared it and hope it would be helpful for Linux learners.
HOW-to zip multiple files
[root@linuxguideco backup]# zip linuxguide_file.zip linuxguide_file1 linuxguide_file2
updating: linuxguide_file1 (deflated 99%)
adding: linuxguide_file2 (deflated 99%)
[root@linuxguideco backup]#
HOW-to zip a directory and its files recursively
[root@linuxguideco /]# zip -r /backup/linuxguide.zip linuxguide
sample output
[root@linuxguideco backup]# ls -l
total 36976
-rw-r--r-- 1 root root 37816934 May 12 11:23 linuxguide.zip
HOW-to unzip a *.zip compressed file
[root@linuxguideco backup]# unzip linuxguide.zip
To see a output during unzip pass the -v option as shown below.
[root@linuxguideco backup]# unzip -v linuxguide.zip
sample output
[root@linuxguideco backup]# unzip -v linuxguide.zip
Archive: linuxguide.zip
Length Method Size Ratio Date Time CRC-32 Name
-------- ------ ------- ----- ---- ---- ------ ----
0 Stored 0 0% 05-12-13 11:17 00000000 linuxguide/
856 Defl:N 628 54% 02-23-12 18:34 de237f42 linuxguide/rmt
1498 Defl:N 685 54% 05-12-13 11:07 f93e84c3 linuxguide/updatesd
-skip-
118 Defl:N 102 14% 05-12-13 11:07 ad292c9a linuxguide/cups
354 Defl:N 201 43% 05-12-13 11:07 3a5bd10d linuxguide/tmpwatch
-------- ------- --- -------
190712477 37281378 81% 3037 files
HOW-to list a content of zip file with uncompressing it
[root@linuxguideco backup]# unzip -l linuxguide.zip
sample output
Archive: linuxguide.zip
Length Date Time Name
-------- ---- ---- ----
0 05-12-13 11:17 linuxguide/
223 05-12-13 11:07 linuxguide/nmptra
877 05-12-13 11:07 linuxguide/mon
-skip-
296 05-12-13 11:07 linuxguide/ntpd
418 05-12-13 11:07 linuxguide/akewha
078 08-29-12 16:28 linuxguide/wat
137 05-12-13 11:07 linuxguide/mlocat
-------- -------
190712477 3037 files
Zip command advanced compression
There are 10 levels of compression by zip command provided.
Level 0 (zero) is the lowest level where there is the archive file without compression.
Level 1 performs little compression. But it will be very fast.
Level 6 is the default level compression.
Level 9 is the maximum compression. It will be slower compared to the default level. In my opinion, if you compress a large file, you should always use level 9.
In the example below, I have level 0 (zero), level 1, default level 6 and level 9 compression to the same directory. See the compressed file size.
[root@linuxguideco /]# zip /backup/linuxguide_file.zip /linuxguide_file
updating: linuxguide_file (deflated 99%)
[root@linuxguideco /]# zip -0 /backup/linuxguide_file-0.zip /linuxguide_file
adding: linuxguide_file (stored 0%)
[root@linuxguideco /]# zip -1 /backup/linuxguide_file-1.zip /linuxguide_file
adding: linuxguide_file (deflated 97%)
[root@linuxguideco /]# zip -6 /backup/linuxguide_file-6.zip /linuxguide_file
adding: linuxguide_file (deflated 99%)
[root@linuxguideco /]# zip -9 /backup/linuxguide_file-9.zip /linuxguide_file
adding: linuxguide_file (deflated 99%)
[root@linuxguideco backup]# ls -lhtr
total 2.7M
-rw-r--r-- 1 root root 1.3M May 14 05:44 linuxguide_file
-rw-r--r-- 1 root root 1.3M May 14 05:39 linuxguide_file-0.zip
-rw-r--r-- 1 root root 41K May 14 05:40 linuxguide_file-1.zip
-rw-r--r-- 1 root root 13K May 14 05:40 linuxguide_file-6.zip
-rw-r--r-- 1 root root 12K May 14 05:41 linuxguide_file-9.zip
-rw-r--r-- 1 root root 13K May 14 05:42 linuxguide_file.zip
[root@linuxguideco backup]#
Validate a Zip Archive
Sometimes you may want to validate a zip archive without extracting. To test the validity of the zip file, pass the option -t as shown below.
[root@linuxguideco backup]# unzip -t linuxguide_file.zip
Archive: linuxguide_file.zip
testing: linuxguide_file OK
No errors detected in compressed data of linuxguide_file.zip.
HOW-to unzip a file
[root@linuxguideco backup]# unzip linuxguide_file-9.zip
Archive: linuxguide_file-9.zip
inflating: linuxguide_file
[root@linuxguideco backup]#
Zip files password protection
Pass the option -P to the zip command to assign a password to the zip file.
[root@linuxguideco backup]# zip -P mysecurepwd linuxguide_file-protected.zip linuxguide_file
adding: linuxguide_file (deflated 99%)
[root@linuxguideco backup]#
[root@linuxguideco backup]# unzip linuxguide_file-protected.zip
Archive: linuxguide_file-protected.zip
[linuxguide_file-protected.zip] linuxguide_file password:
inflating: linuxguide_file
[root@linuxguideco backup]#
The above option is good if you use the command in a shell-script for background work. On the other hand, if you perform the compression on the interactive command line, you do not want to be seen in the password history. So it will use the option -e as shown below to assign a password.
[root@linuxguideco backup]# zip -e linuxguide_file-protected.zip linuxguide_file
Enter password:
Verify password:
adding: linuxguide_file (deflated 99%)
[root@linuxguideco backup]#
When you unzip a password protected file, it will ask for the password, as shown below.
[root@linuxguideco backup]# unzip linuxguide_file-protected.zip
Archive: linuxguide_file-protected.zip
[linuxguide_file-protected.zip] linuxguide_file password:
inflating: linuxguide_file
[root@linuxguideco backup]#
Tar command (tape archive)
tar command is used to convert a group of files into an archive.
The following command created a single archive backup file /backup/abc.tar. This archive will contain all the files and subdirectories.
[root@linuxguideco backup]# tar cvf /backup/abc.tar /abc/
sample output
/abc/abcnss_compat
/abc/abcpamc
/abc/abccap
-skip-
/abc/iptables/abcip6
/abc/iptables/abci
/abc/iptables/policy
[root@linuxguideco backup]#
option t will display all the files from the tar archive.
[root@linuxguideco backup]# tar tvf /backup/abc.tar
drwxr-xr-x root/root 0 2013-04-01 19:35:45 abc/
-rwxr-xr-x root/root 36468 2013-01-08 18:33:16 abc/abcnss
-rwxr-xr-x root/root 9868 2013-01-09 10:30:33 abc/abcpamc
-rw-r--r-- root/root 390824 2009-03-25 06:52:17 abc/abcgobject
-skip-
-rwxr-xr-x root/root 5312 2012-10-30 21:07:16 abc/abcipt_ah
-rwxr-xr-x root/root 8432 2012-10-30 21:07:16 abc/abcip6
-rwxr-xr-x root/root 6932 2012-10-30 21:07:16 abc/abcipL
-rwxr-xr-x root/root 10420 2012-10-30 21:07:16 abc/abcip6
[root@linuxguideco backup]#
x option to extract the files from the tar archive as shown below. This will extract the contents of the current directory location where the command is run.
[root@linuxguideco backup]# tar xvf /backup/abc.tar
abc.tar will be extracted.
[root@linuxguideco backup]# du -hsc /abc.tar
127M /abc.tar
127M total
HOW-to gzip with tar
Add option z to the tar command when dealing with tar.gz compressed file.
[root@linuxguideco backup]# tar cvfz /backup/linuxg.tar.gz /linuxguide
linuxguide/abc.tar
[root@linuxguideco tmp]#
option x will extract all the files from the tar archive.
[root@linuxguideco backup]# tar xvfz /backup/linuxg.tar.gz
option t will display all the files from the tar archive.
[root@linuxguideco backup]# tar tvfz /backup/linuxg.tar.gz
[root@linuxguideco backup]# du -hsc linuxg.tar.gz
60M linuxg.tar.gz
60M total
Note: Using gzip is faster when compared to bzip2.
HOW-to use bzip2 with tar
Add option j to the tar command when dealing with tar.bz2 compressed file.
[root@linuxguideco backup]# tar cvfj /backup/linuxg.tar.bz2 /linuxguide
/tmp/linuxguide/abc.tar
[root@linuxguideco tmp]#
option x will extract all the files from the tar archive.
[root@linuxguideco backup]# tar xvfj /backup/linuxg.tar.bz2
option t will display all the files from the tar archive.
[root@linuxguideco backup]# tar tvfj /backup/linuxg.tar.bz2
Note: Using bzip2 gives higher level of compression when compared to gzip.
In the end, we will summarize this topic what we've learnt from it. Highlights of the article is mentioned below:
how to zip multiple files
zip a directory and its files recursively
unzip a *.zip compressed file
unzip a zip file with v option
list a content of zip file with l option
zip command with advanced compression
validate a zip archive with t option
zip files with password protection
tar (tape archive) command
how to use gzip and bzip2 with tar
We've prepared it and hope it would be helpful for Linux learners.
No comments:
Post a Comment