Gzip Command in Linux
Introduction
The gzip
command in Linux is used to compress files or directories. It creates a compressed version of one or more files, reducing their size and saving disk space. The compressed files typically have the extension .gz
. The gzip
command is commonly used for compressing files to reduce their transfer time and storage requirements.
We'll show you how to use the gzip
command in this tutorial. We will also address a few FAQs on Gzip command in Linux.
gzip
Command Syntax
Following is the syntax of gzip
command:
gzip [OPTION]... [FILE]...
Gzip compresses only individual files, creating a compressed file for each one. A file compressed with Gzip should end in either .gz
or .z
, according to the convention.
You must first construct a Tar archive and then compress the .tar
file with Gzip
if you wish to compress several files or directories into one file. A file with the extension .tar.gz
or .tgz
is a Gzip-compressed Tar archive.
Text files, Tar archives, and web pages are all compressed with Gzip. Images, audio, PDF documents, and other binary items should not be compressed with Gzip because they are already compressed.
Only ordinary files can be compressed with gzip
. The symbolic linkages are not taken into account.
Compress Files with gzip
Use the gzip
command followed by the filename to compress a single file:
gzip filename
gzip
will generate a file named filename.gz
and overwrite the original.
Gzip saves the original file timestamp, mode, ownership, and name in the compressed file by default.
Keep the Original File
Use the -k
option if you wish to keep the input (original) file:
gzip -k filename
Using the -c
option, which tells gzip
to write on standard output and redirect the output to a file, is another way to keep the original file:
gzip -c filename > filename.gz
Verbose output
If you wish to observe the percentage decrease and the names of the files being processed, use the -v
option:
gzip -v filename
Output
filename: 7.5% -- replaced with filename.gz
Compress Multiple Files
You can also use the command to pass several files as arguments. To compress the files file1
, file2
, and file3
, for example, run the following command:
gzip file1 file2 file3
file1.gz
, file2.gz
, and file3.gz
are the three compressed files created by the command above.
Compress all Files in a Directory
Use the -r
option to compress all files in a directory:
gzip -r directory
gzip
will go over the entire directory structure and compress all the files in the directory and its subdirectories in a recursive manner.
Change the Compression Level
You can use gzip
to set compression levels ranging from 1 to 9. -1
or --fast
denotes the quickest compression speed with the lowest compression ratio, while -9
or --best
denotes the slowest compression speed with the highest compression ratio. The compression level is set to -6
by default.
For instance, to obtain the maximum compression, you'd run:
gzip -9 filename
Compression is a CPU-intensive operation, and the higher the compression level, the longer it takes.
Using Standard Input
Pipe the output of the command to gzip
to create a .gz
file from stdin
. To produce a Gzipped MySQL database backup, run:
mysqldump database_name | gzip -c > database_name.sql.gz
The mysqldump
command's output will be used to feed gzip
.
Decompress Files with gzip
Use the -d
option to decompress a .gz
file:
gzip -d filename.gz
Gunzip
is another command that may be used to decompress a Gzip
file. This is just an alias for gzip -d
:
gunzip filename.gz
You might find that gunzip
is easier to remember than gzip -d
.
Keep the Compressed File
The -k
option, like when compressing a file, tells gzip
to keep the input file, in this case, the compressed file:
gzip -dk filename.gz
Decompress Multiple Files
Pass the filenames to gzip
as arguments to decompress many files at once:
gzip -d file1.gz file2.gz file3.gz
Decompress all Files in a Directory
Gzip
decompresses all files in a given directory recursively when used with the -d
and -r
options:
gzip -dr directory
List the Compressed File Contents
When the -l
option is specified, gzip
displays statistics about the compressed files:
gzip -l filename
The uncompressed file name, compressed and uncompressed sizes, and compression ratio will all be included in the output:
Output
compressed uncompressed ratio uncompressed_name
130 107 7.5% filename
Add the -v
option to receive more information:
gzip -lv filename
Output
method crc date time compressed uncompressed ratio uncompressed_name
defla a9b9e776 Sep 3 21:20 130 107 7.5% filename
FAQs of Gzip Command in Linux
How does the gzip
command work?
The gzip
command replaces the original file with a compressed version, maintaining the same filename with the additional .gz
extension. It uses the DEFLATE compression algorithm to reduce the file size.
How do I use the gzip
command to compress a file?
To compress a file using gzip
, simply specify the filename as an argument. For example, gzip filename
will create a compressed file called filename.gz
.
Can gzip
compress multiple files at once?
No, the gzip
command can compress only one file at a time. However, you can use wildcards like *
to compress multiple files matching a pattern, such as gzip *.txt
to compress all .txt
files in the current directory.
What happens to the original file after compression with gzip
?
By default, the gzip
command replaces the original file with the compressed version. The original file is discarded, and only the compressed version remains.
Can I keep the original files when compressing them with gzip
?
Yes, you can use the -k
or --keep
option with the gzip
command to keep the original file after compression. This option will create a compressed file alongside the original, so both versions will be present.
How can I decompress a file compressed with gzip
?
To decompress a file compressed with gzip
, you can use the gunzip
command or the gzip -d
command. For example, gunzip filename.gz
or gzip -d filename.gz
will decompress the filename.gz
file, resulting in the original file without the .gz
extension.
Is it possible to compress directories with gzip
?
No, the gzip
command can only compress individual files. To compress directories, you can either compress the files within the directory individually or use tools like tar
in conjunction with gzip
to create a compressed tarball.
Conclusion
Use the Gzip command to reduce the size of a file. With gzip
command, you can compress and decompress files.
Consult the Gnu gzip documentation page for more information on the gzip command.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.