The Gzip is the popular compression algorithm to reduce the size of a file with keeping file in original mode, ownership and timestamp. It also known as the
.gz file format. The
gzip utility is used to compress and decompress the files. In this guide, we will show you how to use the gzip command.
gzip Command Syntax
The basic syntax for the
gzip command is as follows:
gzip [OPTION]... [FILE]...
gzip, you can compress single file and generate compressed file for each file. The compressed file will be end with the either
If your requirement is to compress multiple files or folders in to one file, you should first create a Tar archive file and need you can compress the
.tar file with
Usually, the Gzip is used to compress the text files, Tar archives, and web pages. It’s useless to use
gzip for compress images, audio, PDF documents, and other binary files as they are already compressed.
The regular files can be compressed by gzip and the symbolic links are ignored.
Compress Files with gzip
You can compress a single file using the
gzip command followed by the filename:
It will delete the original file and creates a
filename.gz file. Gzip keeps the original file timestamp, mode, ownership, and name in the compressed file.
Keep Original File
If you would like to keep the original file instead of delete after compression, use
-k option with
gzip -k filename
To see the percentage of reduction and the names of the files that are being processed, use the
gzip -v filename
filename: 9.5% -- replaced with filename.gz
Compress multiple files
To compress the multiple files, pass multiple file names as arguments. For example, to compress the files named
filename3, you would run the following command:
gzip filename1 filename2 filename3
As an output the above command will create the three different files named with
Compress all files in a directory
To compress all files in a given directory, use the
gzip -r directory
gzip will recursively traverse through the whole directory structure and compress all the files in the directory and it’s subdirectories.
Change compression level
You can specify the range if compression levels from
9. For fastest compression with minimal compression ratio, you can pass
--fast option. For the maximum compression ratio and slowest compression you should use the
--best option with the
gzip command. By default, the compression level is
For example, for maximum compression, type:
gzip -9 filename
Compression is a CPU-intensive task, the higher the compression level, the longer the process takes.
Using standard input
You can create a
.gz file from the standard input, just pipe the output if the command to
gzip. For example, to generate a Gzip MySQL database backup file, you would run:
mysqldump database | gzip -c > database.sql.gz
The gzip will consider input which is the output of the
Decompressing Files with gzip
-d option, to decompress a
gzip -d filename.gz
You can use the
gunzip command to decompress a Gzip file. This command is basically an alias of
Keep the compressed file
Same as when compressing a file, the
-k option tells
gzip to keep the input file, in this case, that is the compressed file:
gzip -dk filename.gz
Decompress multiple files
You can decompress multiple files in single command by passing the filenames as arguments:
gzip -d filename1.gz filename2.gz filename3.gz
Decompress all files in a directory
-r options to decompress all files in a given directory recursively:
gzip -dr directory
Using Gzip you can reduce the size of a given file. The
gzip command allows you to compress and decompress files. To learn more about the
gzip command, visit Gnu gzip documentation page.
If you have any questions or feedback, leave a comment below.
Leave a Reply