Compression of files is a necessity these days. It’s not only helps you in saving your space but also helps you in quick sharing and back up purposes. Text files compresses more than the web images and videos because they are already in the compressed form .You must have observed many open source softwares in the .tar.gz packages. These are the compressed file made out using the tar and gzip (gunzip) utility. So let’s get started with these great utilities 😀
Tar : Good practice for compressing a file is to archive the files first and then compress them. So to create an archive of a disk archive that contains a group of files or an entire directory , we should use the tar utility. Another advantage of the tar is that it also compresses the file to some extent. So using tar with gzip means double layer compression. To use the tar we should need to know some of its options:
-c : Create an archive
-x : Extract from the archive
-t : Display the files in archive
-f : Specify the archive named as arch
Create the archive : You can create the archive by following the syntax tar -cvf file.tar file1 file2 . For Example- We created an archive named as html.tar for the html folder by using the command tar -cvf html.tar html . As I told you above tar compresses the file also. You can easily see this in the below screenshot :
Extracting files from tar archive : You can extract the files for tar archive using the ‘-x’ utility. Follow this syntax and your file will get extracted tar -xvf file.tar
Gzip or Gunzip : Using this utility on the tar archive file adds an another layer of compression. As a result of both you got the .tar.gz file format. Challenger of the gzip is the bzip2 utility because it compresses the file in the greater ratio. But bzip2 is a slow compression process. So it depends on your needs which utility to use.
Compress using gzip : You can compress with the gzip using the simple syntax of gzip filename . We are compressing the html.tar archive further using the gzip . You can also use the ordinary files in the filename instead of the archive. We can see the compression ratio by using ‘-l’ option along with this utility. Below is the screenshot of the same :
Extracting the gzip compression : You can easily extract the gzip utility by using either the “gunzip” command or by using the ‘-d’ option with the gzip. In short you need to follow the following syntax : gzip -d filename.gz or gunzip filename.gz.
I hope this tutorial will be useful for you. Suggestions,feedback and queries are always welcome.