gzip Command in Linux

When you need to reduce file sizes before transferring them over a network or freeing up disk space, gzip is the standard compression tool on Linux.
gzip uses the Deflate algorithm and saves compressed files with a .gz extension. By default, it replaces the original file with the compressed version and keeps the file’s timestamp, mode, and ownership intact.
gzip compresses only single files. If you want to compress multiple files or a directory into one archive, you need to create a Tar archive
first and then compress the .tar file with gzip. Files ending in .tar.gz or .tgz are Tar archives compressed with gzip.
gzip is best suited for text files, Tar archives, and similar data. Avoid using it on images, audio, PDFs, and other binary formats that are already compressed; the size reduction will be negligible.
This guide covers the most common gzip options with practical examples.
gzip Command Syntax
The general syntax for the gzip command is as follows:
gzip [OPTION]... [FILE]...gzip compresses only regular files and creates a compressed file for each input. By convention, compressed files end with .gz or .z. Symbolic links
are ignored.
Compressing Files with gzip
To compress a single file, invoke the gzip command followed by the filename:
gzip filenamegzip will create a file filename.gz and delete the original file.
By default, gzip keeps the original file timestamp, mode, ownership, and name in the compressed file.
Keep the original file
If you want to keep the input (original) file, use the -k option:
gzip -k filenameAnother option to keep the original file is to use the -c option, which tells gzip to write on standard output and redirect the output to a file:
gzip -c filename > filename.gzThis method is useful in scripts because it never removes the original file.
Verbose output
Use the -v option if you want to see the percentage reduction and the names of the files that are being processed:
gzip -v filenamefilename: 7.5% -- replaced with filename.gzCompress multiple files
You can also pass multiple files as arguments to the command. For example, to compress the files named file1, file2, file3, you would run the following command:
gzip file1 file2 file3The command above will create three compressed files, file1.gz, file2.gz, file3.gz.
Compress all files in a directory
To compress all files in a given directory, use the -r option:
gzip -r directorygzip will recursively traverse through the whole directory structure and compress all the files in the directory and its subdirectories.
Change the compression level
gzip allows you to specify a range of compression levels, from 1 to 9. -1 or --fast means the fastest compression speed with minimal compression ratio, and -9 or --best indicates the slowest compression speed with maximum compression ratio. The default compression level is -6.
For example, to get maximum compression, you would run:
gzip -9 filenameCompression is a CPU-intensive task; the higher the compression level, the longer the process takes.
Using standard input
To create a .gz file from standard input, pipe the output of the command to gzip. For example, to create a Gzipped MySQL database backup
, you would run:
mysqldump database_name | gzip -c > database_name.sql.gzThe output of the mysqldump command will be input for gzip.
Decompressing Files with gzip
To decompress a .gz file
, use the -d option:
gzip -d filename.gzAnother command you can use to decompress a Gzip file is gunzip
. It works the same as gzip -d:
gunzip filename.gzYou might find it easier to remember gunzip than gzip -d.
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.gzDecompress multiple files
To decompress multiple files at once, pass the filenames to the gzip command as arguments:
gzip -d file1.gz file2.gz file3.gzDecompress all files in a directory
When used with -d and -r options, gzip decompresses all files in a given directory recursively:
gzip -dr directoryList Compressed File Information
When used with the -l option, the gzip command shows statistics about the given compressed files:
gzip -l filenameThe output will include the uncompressed file name, the compressed and uncompressed size, and the compression ratio:
compressed uncompressed ratio uncompressed_name
130 107 7.5% filenameTo get more information, add the -v option:
gzip -lv filenamemethod crc date time compressed uncompressed ratio uncompressed_name
defla a9b9e776 Sep 3 21:20 130 107 7.5% filenameTest a Compressed File
To verify a .gz file without extracting it, use the -t option:
gzip -t filename.gzView the Contents of a Compressed File
To view the contents of a .gz file without decompressing it, use zcat:
zcat filename.gzzcat writes the decompressed output to standard output, so you can pipe it to other commands. For example, to search a compressed log file for a pattern:
zcat access.log.gz | grep 'ERROR'Quick Reference
For a printable quick reference, see the gzip cheatsheet .
| Command | Description |
|---|---|
gzip filename | Compress a file (replaces original) |
gzip -k filename | Compress and keep the original file |
gzip -d filename.gz | Decompress a file |
gunzip filename.gz | Decompress a file (alias for gzip -d) |
gzip -r directory | Compress all files in a directory recursively |
gzip -9 filename | Maximum compression |
gzip -1 filename | Fastest compression |
gzip -l filename.gz | Show compression statistics |
gzip -t filename.gz | Test integrity of a compressed file |
zcat filename.gz | View compressed file contents without extracting |
Conclusion
gzip is a fast and widely supported compression tool, and understanding its options makes everyday tasks like archiving logs or preparing files for transfer much quicker. For more details, run man gzip or visit the GNU gzip documentation
.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page