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.

Example :

Let us say we have a folder called temp. The size of the folder can be found using the command du.

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

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.

We can see that the original file "temp.tar" has been replaced by the compressed file.

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

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"

Example :

While decompressing the files that are compressed with custom suffix we need to pass the suffix also.

If we want to find out the size of the decompressed file before decompressing we can use the option "-l".

Follow by Email