Linux Commands

How to Use Gzip on Linux?

Gzip is a common compression algorithm that allows you to compress a file while keeping the original file format, ownership, and timeline.

When you use the gzip command to compress a file or folder, it would have the same name as before, but with the extension.gz.

Syntax:

gzip Option File_name

Text files, Tar archives, and websites are all compressed with Gzip.

Compress File Using .gz:

Enter the mentioned command to compress a single file with .gz:

$ gzip filename

A new zip file will be created with .gz extension.

Compress Multiple Files:

You can compress multiple files simultaneously, pass filenames as parameters. Run the below command:

$ gzip file1name file2name file2name

Keep Original File Save:

The original file will be deleted after creating the .gz file. Use –k option to keep the original file:

$ gzip –k file_name

Alternatively, if you want to preserve the original file, you can use the -c option which allows gzip to write to an output file and pass it to a file.

$ gzip –c filename > filename.gz

Compress Every File in a Folder and its Subfolder:

Use the following command to compress all files in a folder and subfolders.

$ gzip –r foldername

Decompress File Using .gz:

Use –d option to decompress .gz archive. Follow the below command:

$ gzip –d filename.gz

Another way to decompress a .gz file is by using gunzip command:

$ gunzip filename.gz

Decompress Multiple Files:

We can decompress multiple files simultaneously using the command shown below:

$ -d file1name file2name file3name file4name.gz

Test Validity of File:

Use –t option to test the validity of the compressed file.

$ gzip –t filename

Change Compression Level:

You may use gzip to define compression levels ranging from 1 to 9. You can select between a smaller compression level that runs faster or a maximum compression level that takes longer to run.

Enter the following command to get a full compression at the lowest latency:

$ -9 file_name

Enter the below-mentioned command to get minimum compression for a faster speed:

$ -1 file_name

List the Content of the Compressed File:

Use –l option to show the statistics of the compressed ad uncompressed file.

Use the –v option with -l to get more detail of the compressed file.

$ gzip –lv filename

Conclusion:

You may use gzip to compress a file and make it smaller. The gzip command can be used to either compress or decompress files. Multiple files can also be compressed or decompressed at the same time. We have used various options of gzip.

About the author

Avatar

Aqsa Maqbool

As a Software engineer, I am passionate to write about various IT related
articles but have deep interest in Linux. I spend most of my time reading Linux related blogs and IT related books. I want to serve the world with my writing skills.