gzip: Compressing files using gzip
gzip is a command that can be used to compress and decompress files. The command takes one or more files as input and compresses each of them separately.
The new compressed file is created with a “.gz” extension, i.e. if we compressed the file “temp”, the compressed file will be named temp.gz and the compressed file will replace the original file.
The command works only on files and if we want to compress folders using gzip , we can convert a folder into a single “tar” archive and use the gzip command on the archive.
Let us say we have a folder called temp. The size of the folder can be found using the command du.
$ du -h temp 52M temp
We can convert the folder into a “.tar” file using the command tar
Note : Using tar command is given in the post :Using tar to archive files
$ tar cf temp $ du -h temp.tar 52M temp
We can see that the “tar” command only archives but does not compress the file.Now we can use the gzip command on the tar file to compress it.
$ gzip temp.tar $ ls temp.tar.gz
We can see that the original file “temp.tar” has been replaced by the compressed file.
$ du -h temp.tar.gz 38M temp
The size of the compressed file is much smaller than the original file, thus clear that gzip compresses the file
The files compressed using gzip can be decompressed using gzip with option “-d” or with the command guzip. By default gzip creates the decompressed file with the name of the original file with “.gz” removed i.e temp.tar.gz will become temp.tar and will replace the original temp.tar.gz Example
$ gzip -d temp.tar.gz gzip: temp.tar already exists; do you wish to overwrite (y or n)?
If temp.tar already exists in the folder gzip prompts the uses to confirm ifthe previous file has to be overwritten, and only on reciveing the confirmation will it overwrite the existing file
We can also change the suffix from “.gz” to any other characters we wish by using the option “-S”
$ gzip -S “.zipped” temp.tar $ ls temp.tar.zipped
While decompressing the files that are compressed with custom suffix we need to pass the suffix also.
$ gunzip temp.tar.zipped gzip: temp.tar.zipped: unknown suffix — ignored $ gunzip -S “.zipped” temp.tar.zipped $ ls temp.tar
If we want to find out the size of the decompressed file before decompressing we can use the option “-l”.
$ gzip -l temp.tar.gz compressed uncompressed ratio uncompressed_name 39195219 53708800 27.0% temp.tar.zipped