Linux gzip Command examples
gzip is one of the compression method in linux. It
compresses the normal file and stores into single .gz extension file,
this compression is much better compare with zip compression.
Syntax:
Common syntax for gzip and gunzip.# gzip (Compress) syntax #
root@2daygeek [~]# gzip [file to be compressed]
# gzip (Decompress) syntax #
root@2daygeek [~]# gunzip [.gz file name]
# gzip (Decompress) syntax #
root@2daygeek [~]# gzip -d [.gz file name]
1) How to compress the file using gzip command
The below example compress the “test-gzip.txt” file to test-gzip.txt.gz file. See the output.# File compression #
# gzip test-gzip.txt
# Checking compressed file #
# ls -lh /home/mageshm/test-gzip.txt.gz
-rw-r--r--. 1 root root 92 Dec 27 13:31 test-gzip.txt.gz
2) How to view gzip/compressed file content without extracting
The below command is show the gzip/compressed file contents without extracting. See the output.# gzip -l test-gzip.txt.gz
compressed uncompressed ratio uncompressed_name
92 60 13.3% test-gzip.txt
3) Unzip/Decompress the gzip file
The below command is Unzip/Decompress the gzip file.# gunzip test-gzip.txt.gz
or
# gzip -d test-gzip.txt.gz
4) Compress the file/folder recursively
The below example compress the “link” folder recursively using gzip command.# gzip -r linksHere i’m going to check the mentioned folder was compressed or not. It seems that the link folder is shown as a folder. I’m confused to move it inside the links directory as the gzip command compresses only the files recursively and stores their original location.
# ll -h
total 56K
drwx------. 6 mageshm mageshm 4.0K Dec 27 14:04 ./
drwx--x--x. 16 root root 4.0K Dec 24 01:16 ../
-rw-------. 1 mageshm mageshm 1.2K Dec 26 14:09 .bash_history
-rw-r--r--. 1 mageshm mageshm 18 May 10 2012 .bash_logout
-rw-r--r--. 1 mageshm mageshm 176 May 10 2012 .bash_profile
-rw-r--r--. 1 mageshm mageshm 124 May 10 2012 .bashrc
-rw-r--r--. 1 root root 26 Dec 27 12:25 liks
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:13 links/
drwxrwxr-x. 2 mageshm mageshm 4.0K Nov 16 10:14 perl5/
drwxr-xr-x. 2 mageshm mageshm 4.0K Nov 16 10:07 public_ftp/
drwxr-xr-x. 3 mageshm mageshm 4.0K Nov 16 10:07 public_html/
lrwxrwxrwx. 1 root root 31 Dec 7 09:41 s-link-file -> /links/soft-link/test-soft-link
lrwxrwxrwx. 1 root root 38 Dec 7 09:42 s-link-folder -> /links/soft-link/test-soft-link-folder
-rw-r--r--. 1 root root 61 Dec 27 13:33 test1-gzip.txt
-rw-r--r--. 1 root root 61 Dec 27 13:35 test2-gzip.txt
-rw-r--r--. 1 root root 60 Dec 27 13:31 test-gzip.txt
Links folder contain 2 folders and 3 files recursively, the gzip
command compress all the files recursively and stored their location.
See the output.# ll -h total 16K drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 ./ drwx------. 6 mageshm mageshm 4.0K Dec 27 14:04 ../ drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 soft-link/ -rw-r--r--. 1 root root 92 Dec 27 14:10 test-gzip.txt.gz
root@server57324 [/home/mageshm/links/soft-link]# ll -h total 16K drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 ./ drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 ../ -rw-r--r--. 1 root root 94 Dec 27 14:10 test2-gzip.txt.gz drwxr-xr-x. 2 mageshm mageshm 4.0K Dec 27 14:14 test-soft-link-folder/
root@server57324 [/home/mageshm/links/soft-link/test-soft-link-folder]# ll -h total 12K drwxr-xr-x. 2 mageshm mageshm 4.0K Dec 27 14:14 ./ drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 ../ -rw-r--r--. 1 root root 94 Dec 27 14:10 test1-gzip.txt.gz
5) How to view the gzip/compressed file/folder recursively
Again i’m going to check the gzip/compressed file contents without extracting.# less links total 16 drwxr-xr-x. 3 mageshm mageshm 4096 Dec 27 14:14 ./ drwx------. 6 mageshm mageshm 4096 Dec 27 14:04 ../ drwxr-xr-x. 3 mageshm mageshm 4096 Dec 27 14:14 soft-link/ -rw-r--r--. 1 root root 92 Dec 27 14:10 test-gzip.txt.gz links (END)The output clearly shows the file and folders recursively compressed.
6) How to unzip/compressed files/folders recursively
The below example is unzip the files/folders recursively.# gunzip -r linksOnce again i’m going to check whether the files/ folder unzip/de-compressed recursively.
# ll -h
total 16K
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 ./
drwx------. 6 mageshm mageshm 4.0K Dec 27 14:04 ../
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 soft-link/
-rw-r--r--. 1 root root 92 Dec 27 14:10 test-gzip.txt
root@server57324 [/home/mageshm/links/soft-link]# ll -h total 16K drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 ./ drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 ../ -rw-r--r--. 1 root root 94 Dec 27 14:10 test2-gzip.txt drwxr-xr-x. 2 mageshm mageshm 4.0K Dec 27 14:14 test-soft-link-folder/
root@server57324 [/home/mageshm/links/soft-link/test-soft-link-folder]# ll -h
total 12K
drwxr-xr-x. 2 mageshm mageshm 4.0K Dec 27 14:14 ./
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 27 14:14 ../
-rw-r--r--. 1 root root 94 Dec 27 14:10 test1-gzip.txt
Comments
Post a Comment