Linux Shell Commands - Part 6


In this post I will discuss about commands used for compressing and decompressing files.




1. 'gzip' used for compressing files. 'gunzip' used for decompressing files. Files compressed with 'gzip' are saved with '.gz' extension.

    a. 'gzip' with no keys, compress a file and removes the original file.

   
root@bt:~/Desktop/Ckorner/post6# ls -l
    total 16
    -rw-r--r-- 1 root root 164 2012-07-12 11:47 post6.gz
    -rw-r--r-- 1 root root 206 2012-07-12 11:45 post6.txt
    -rw-r--r-- 1 root root 206 2012-07-12 11:54 postuncmprsd.txt
    -rw-r--r-- 1 root root   2 2012-07-12 11:40 shellcommands6
   
root@bt:~/Desktop/Ckorner/post6# gzip postuncmprsd.txt
   
root@bt:~/Desktop/Ckorner/post6# ls -l
    total 16
    -rw-r--r-- 1 root root  164 2012-07-12 11:47 post6.gz
    -rw-r--r-- 1 root root  206 2012-07-12 11:45 post6.txt
    -rw-r--r-- 1 root root  171 2012-07-12 11:54 postuncmprsd.txt.gz
    -rw-r--r-- 1 root root    2 2012-07-12 11:40 shellcommands6
   
root@bt:~/Desktop/Ckorner/post6#

    Note here 'postuncmprsd.txt' is no longer there.


    b. To compress 'gzip -c [original file] > [compressed_file.gz]

   
root@bt:~/Desktop/Ckorner/post6# ls -l
    total 8
    -rw-r--r-- 1 root root 206 2012-07-12 11:45 post6.txt
    -rw-r--r-- 1 root root   2 2012-07-12 11:40 shellcommands6
   
root@bt:~/Desktop/Ckorner/post6# gz
    gzexe  gzip  
   
root@bt:~/Desktop/Ckorner/post6# gzip -c post6.txt > post6.gz
   
root@bt:~/Desktop/Ckorner/post6# ls -l
    total 12
    -rw-r--r-- 1 root root 164 2012-07-12 11:47 post6.gz
    -rw-r--r-- 1 root root 206 2012-07-12 11:45 post6.txt
    -rw-r--r-- 1 root root   2 2012-07-12 11:40 shellcommands6
   
root@bt:~/Desktop/Ckorner/post6#

    Note with 'gzip -c' the original file stays.

    c. 'gzip -l file.gz' reveals the status of a compressed file

   
root@bt:~/Desktop/Ckorner/post6# gzip -l post6.gz
        compressed        uncompressed  ratio uncompressed_name
            164                 206  37.9% post6
   
root@bt:~/Desktop/Ckorner/post6#

    d. 'gzip -d [compressedfile.gz]' decompress a compressed file, but deletes the compressed file after decompressing.

   
root@bt:~/Desktop/Ckorner/post6# ls -la
    total 24
    drwxr-xr-x 2 root root 4096 2012-07-12 11:56 .
    drwxr-xr-x 9 root root 4096 2012-07-12 11:39 ..
    -rw-r--r-- 1 root root  164 2012-07-12 11:47 post6.gz
    -rw-r--r-- 1 root root  206 2012-07-12 11:45 post6.txt
    -rw-r--r-- 1 root root  171 2012-07-12 11:54 postuncmprsd.txt.gz
    -rw-r--r-- 1 root root    2 2012-07-12 11:40 shellcommands6
   
root@bt:~/Desktop/Ckorner/post6# gzip -d postuncmprsd.txt.gz
   
root@bt:~/Desktop/Ckorner/post6# ls -l
    total 16
    -rw-r--r-- 1 root root 164 2012-07-12 11:47 post6.gz
    -rw-r--r-- 1 root root 206 2012-07-12 11:45 post6.txt
    -rw-r--r-- 1 root root 206 2012-07-12 11:54 postuncmprsd.txt
    -rw-r--r-- 1 root root   2 2012-07-12 11:40 shellcommands6
   
root@bt:~/Desktop/Ckorner/post6#

    Note, 'postuncmprsd.txt.gz' is no longer there.

    e. gzip -d -c [compressedfile.gz] will decompress and dump the contents to screen, and will not delete the original compressed file, where as if a redirectional operator is used, the contents of compressed file will be stored to a new file.
    gzip -d -c [compressedfile.gz] > uncompressed.txt

   
root@bt:~/Desktop/Ckorner/post6# ls -l
    total 16
    -rw-r--r-- 1 root root 164 2012-07-12 11:47 post6.gz
    -rw-r--r-- 1 root root 206 2012-07-12 11:45 post6.txt
    -rw-r--r-- 1 root root 171 2012-07-12 11:54 postuncmprsd.txt.gz
    -rw-r--r-- 1 root root   2 2012-07-12 11:40 shellcommands6
   
root@bt:~/Desktop/Ckorner/post6# gzip -dc postuncmprsd.txt.gz > uncomprsdfile.txt
   
root@bt:~/Desktop/Ckorner/post6# ls -l
    total 20
    -rw-r--r-- 1 root root 164 2012-07-12 11:47 post6.gz
    -rw-r--r-- 1 root root 206 2012-07-12 11:45 post6.txt
    -rw-r--r-- 1 root root 171 2012-07-12 11:54 postuncmprsd.txt.gz
    -rw-r--r-- 1 root root   2 2012-07-12 11:40 shellcommands6
    -rw-r--r-- 1 root root 206 2012-07-12 12:04 uncomprsdfile.txt
   
root@bt:~/Desktop/Ckorner/post6#

    Note here, after decompressing the original file stays.

    f. 'less [compressedfile.gz]' is a quick way to check the contents of a compressed file. 'less' is known as paginator command. It displays contents of a file page by page

   
root@bt:~/Desktop/Ckorner/post6# # less postuncmprsd.txt.gz

    [output ommited], to exit from Paginator, ctrl+z

    g. 'zcat [compresdfile.gz]' dumps the contents to STDOUT(Screen), and if used with redirectional operator, the contents will be stored in the given file.

   
root@bt:~/Desktop/Ckorner/post6# ls -l
    total 20
    -rw-r--r-- 1 root root 164 2012-07-12 11:47 post6.gz
    -rw-r--r-- 1 root root 206 2012-07-12 11:45 post6.txt
    -rw-r--r-- 1 root root 171 2012-07-12 11:54 postuncmprsd.txt.gz
    -rw-r--r-- 1 root root   2 2012-07-12 11:40 shellcommands6
    -rw-r--r-- 1 root root 206 2012-07-12 12:04 uncomprsdfile.txt
   
root@bt:~/Desktop/Ckorner/post6# zcat postuncmprsd.txt.gz > uncmpsdwithzcat.txt
   
root@bt:~/Desktop/Ckorner/post6# ls -l
    total 24
    -rw-r--r-- 1 root root 164 2012-07-12 11:47 post6.gz
    -rw-r--r-- 1 root root 206 2012-07-12 11:45 post6.txt
    -rw-r--r-- 1 root root 171 2012-07-12 11:54 postuncmprsd.txt.gz
    -rw-r--r-- 1 root root   2 2012-07-12 11:40 shellcommands6
    -rw-r--r-- 1 root root 206 2012-07-12 12:16 uncmpsdwithzcat.txt
    -rw-r--r-- 1 root root 206 2012-07-12 12:04 uncomprsdfile.txt
    root@bt:~/Desktop/Ckorner/post6#

    Note here, the original file stays.

Some extra tips:

Commands that takes more than one keys like -d -c -v etc for 'gzip', all the keys can be bundled

Ex: gzip -c9v sometext.txt > sometextfilecmpressed.gz

is same as:
gzip -c -9 -v sometext.txt > sometextfilecmpressed.gz

Useful Keys with gzip:

-v --> turns on the verbose mode
-1 --> faster compression, less CPU utilization, but compression ratio is less
-9 --> better compression, more CPU utilization, but compression ratio is more



Okay so these are daily useful commands needed for 'gzip' but if you want to know more about 'gzip' please check the man page or use the --help key.

Thats all for this post, stay tuned for more.

Feel Free To Leave A Comment If Our Article has Helped You, Support Us By Making A Small Contribution, Thank You!


0 comments: