In this article we are going to shows how to use gzip command in Linux. If you want to learn about gzip command in Linux with Examples then this post is ideal for you.
In the Linux, gzip is a file format( .gz
) and a software utility that is used for file compression and decompression. It is one of the most used and popular compression algorithms that is used to reduce the size of a given file and keep the original file mode, ownership and timestamp.
How to Use the gzip Command in Linux
In the Linux, the basic syntax of the gzip command is show as below:
gzip [OPTION]... [FILE]...
Gzip utility compress only single files and creates a compressed file for each given file. The compressed file using gzip command will be end with the either .gz
or .z
extension. It is mostly used to compress text files, Tar archives, and web pages.
Compress Files with gzip Command
To compress a file with gzip command, run the command below with the file name:
$ gzip filename.txt
The above command will create a filename.txt.gz
file and will delete the original file.
Keep Original File
Use -k
option with gzip
command, if you want to keep the original file. Run the command below to do that:
$ gzip -k filename.txt
Verbose output
If you want to display the percentage of reduction and the names of the files that are being compressed or decompressed, use the -v
option:
$ gzip -v filename.txt
Output
filename.txt: 9.5% -- replaced with filename.txt.gz
Use multiple files with gzip Command
Gzip Command also allows you to use multiple files as arguments. To compress multiple files at once, run the command below:
$ gzip filename1.txt filename2.txt filename3.txt
The above command will create three different compressed files named file1.txt.gz
, file2.txt.gz
, file3.txt.gz
.
Compress all Files in specified a Directory
To compress all files in a specified directory, use the -r
option with gzip command:
$ gzip -r directory
Change compression level
Gzip command also allows you to specify the range (1-9) of compression level. Use -1
or --fast
option for fastest compression with minimal compression ratio and Use -9
or --best
option for the maximum compression ratio and slowest compression. By default, the compression level is set as -6
.
For maximum compression ratio with slowest compression, run the command below:
$ gzip -9 filename.txt
Compress a file using standard input
For example, if you you want to create a MySQL database backup file with gzip compression, Run the command below:
$ mysqldump database_backup | gzip -c > database_backup.sql.gz
Decompress Files with gzip Command
Use the -d
option, If you want decompress a .gz
compressed file:
$ gzip -d filename.txt.gz
You can also use gunzip
command to decompress a Gzip file.
Keep the Compressed file during Decompressing
Same as the compressing a file, use the -d
and -k
option to keep the input or original file:
$ gzip -dk filename.txt.gz
Decompress multiple files with gzip Command
To decompress multiple files at once, run the command below:
$ gzip -d filename1.txt.gz filename2.txt.gz filename3.txt.gz
Decompress all files in a specified directory
To decompress all files in a specified directory, use -d
and -r
options:
$ gzip -dr directory
That’s all.
If you face any error and issue in above steps , please use comment box below to report.
If our tutorials helped you, please consider buying us a coffee. We appreciate your support!
Thank you for your support.