Tuesday, 26 January 2016

How To Zip/Unzip A File In Unix/Linux

In this article we will see how to zip/compress files as well as how to unzip/decompress the files. We will divide this article in three categorise as explained below:

Category:1-> Switches which will be applied on Normal files (unzipped file)
Category:2-> Switches which will be applied on zipped files.
Category:3-> gunzip to unzip the files.

We will see some of the below listed switches, you can get the list from man gzip or gzip -h.

-d, --decompress  decompress
-h, --help        give this help
-k, --keep        keep (don't delete) input files
-l, --list        list compressed file contents
-q, --quiet       suppress all warnings
-r, --recursive   operate recursively on directories
-t, --test        test compressed file integrity
-v, --verbose     verbose mode
-1, --fast        compress faster
-9, --best        compress better

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<CATEGORY:1>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

ZIP The Input File

$ gzip F_Input_File1.txt

Output: Will create a zip file
F_Input_File.txt1.gz

We can zip multiple files together as shown below:
$ gzip F_Input_File1.txt F_Input_File2.txt F_Input_File3.txt

Keep The Original File With Zipped File

The original file be lost once the zip file is created,if you want to keep the original file with the zipped file, use -k switch.

$ gzip -k F_Input_Fil1e.txt

Output: Will zip the file and keep the original file as well.
$ ls
F_Input_File1.txt
F_Input_File1.txt.gz

What gzip is Doing

$ gzip -v F_Input_File1.txt

Output: It will provide real time activities(verbose) which gzip is doing.

Zip All The File Recursively

$ gzip -v -r /home/baba

Output: The -r switch has forced gzip to perform recursive compression(zip).
/home/baba/F_Input_File1.txt:    11.1% -- replaced with /home/baba/F_Input_File1.txt.gz
/home/baba/F_Input_File2.txt:    51.6% -- replaced with /home/baba/F_Input_File2.txt.gz
/home/baba/Imp/F_Input_File1.txt:       -12.5% -- replaced with /home/baba/Imp/F_Input_File1.txt.gz

Zip All The File Recursively, Keeping Original Files

We can also do recursive zip keeping the original files.

$ gzip -v -k -r /home/baba

Output:
/home/baba/F_Input_File1.txt:    11.1% -- replaced with /home/baba/F_Input_File1.txt.gz
/home/baba/F_Input_File2.txt:    51.6% -- replaced with /home/baba/F_Input_File2.txt.gz
/home/baba/Imp/F_Input_File1.txt:       -12.5% -- replaced with /home/baba/Imp/F_Input_File1.txt.gz

$ ls -ltr
total 7
-rw-r--r--  1 baba None   52 Jan 26 12:50 F_Input_File1.txt.gz
-rw-r--r--  1 baba None   36 Jan 26 12:50 F_Input_File1.txt
-rw-r--r--  1 baba None  604 Jan 26 13:08 F_Input_File2.txt.gz
-rw-r--r--  1 baba None 1206 Jan 26 13:08 F_Input_File2.txt
drwxr-xr-x+ 1 baba None    0 Jan 26 13:24 Imp

How To Do Fast Zipping/Compression

$ gzip -v -1 F_Input_File1.txt
[OR]
$ gzip -v --fast F_Input_File1.txt

How To Do best Zipping/Compression

$ gzip -v -9 F_Input_File1.txt
[OR]
$ gzip -v --best F_Input_File1.txt

-1 or --fast indicates the fastest compression method (less compression) and -9 or --best indicates the slowest compression method (best compression). The default compression level is -6 (that is, biased towards high compression at expense of speed).

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<CATEGORY:2>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Unzip/Decompress The Zipped file

We have a separate command, gunzip, to unzip the files but we can do the same by using gzip itself. We will see gunzip as well.

$ gzip -d F_Input_File.txt.gz
$ gzip --decompress F_Input_File.txt.gz
$ gzip --uncompress F_Input_File.txt.gz

Output: All the 3 will unzip the file.

Unzip The Zipped Files Recursively

$ gzip -v -d -r /home/baba

Output: -d switch will unzip, -r switch will perform recursion, -v verbose
/home/baba/F_Input_File1.txt.gz:         11.1% -- replaced with /home/baba/F_Input_File1.txt
/home/baba/F_Input_File2.txt.gz:         51.6% -- replaced with /home/baba/F_Input_File2.txt
/home/baba/Imp/F_Input_File1.txt.gz:    -12.5% -- replaced with /home/baba/Imp/F_Input_File1.txt

Statistics Of Zipped File

$ gzip -v -l F_Input_File.txt.gz

Output: Will lsit down the statistics of zipped file.
method  crc     date  time           compressed        uncompressed  ratio uncompressed_name
defla 69cf461f Jan 26 12:50                  52                  36  11.1% a

How To Read A Zip/Compressed File

$ gcat F_Input_File1.txt.gz

Output: Will display the content of the file F_Input_File1.txt

How To Check Whether File Is Compressed Correctly

$ gzip -v -t F_Input_File1.txt.gz

Output: -t switch is used to test the integrity.
F_Input_File1.txt.gz:    OK

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<CATEGORY:3>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

We can use Most of the switches of gzip in gunzip will see the same in upcoming example.

Unzip/ Decompress The Zipped File using gunzip 

$ gunzip F_Input_File1.txt.gz
$ gunzip -v F_Input_File1.txt.gz

Output: Will unzip the file
F_Input_File1.txt

$ gunzip -d F_Input_File1.txt.gz
$ gunzip -v -d F_Input_File1.txt.gz

Output: -d switch will decompress the zip file, we have already seen the same with gzip file.
F_Input_File1.txt

Unzip/ Decompress The Zipped File Recursively using gunzip 

$ gunzip -r /home/baba/
$ gunzip -v -r /home/baba/

Output: Will unzip/decompress all the zipped files recursively.

Unzip/ Decompress The Zipped File, keeping the original zipped files.

$ gunzip -k F_Input_File1.txt.gz F_Input_File2.txt.gz
$ gunzip -v -k F_Input_File1.txt.gz F_Input_File2.txt.gz

Output: Will unzip the files as well as keep the copy of zip files. 

Note: If you observe we gzip and gunzip are very similar in functionality as well as in working. We should always use -v (verbose option), it provides the clear picture about what is going on.

Conclusion: Go with anyone gzip or gunzip, They are almost doing same kind of thing so why to use both, master one!!!

Keep Reading, Keep Learning, keep Sharing...!!!

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...