gzip Command in Linux

By 

•

Updated on

•

8 min read

Linux Gzip Command

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:

txt
gzip [OPTION]... [FILE]...

gzip compresses only regular files and creates a compressed file for each input. Compressed files get the .gz extension. To use a different suffix, pass it with the -S option. Symbolic links are ignored.

Compressing Files with gzip

To compress a single file, invoke the gzip command followed by the filename:

Terminal
gzip filename

gzip will create a file filename.gz and delete the original file.

Keep the original file

If you want to keep the input (original) file, use the -k option:

Terminal
gzip -k filename

Another way to keep the original file is the -c option. It tells gzip to write the compressed data to standard output. Redirect the output to a file:

Terminal
gzip -c filename > filename.gz

This 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. For example, to compress a copy of the /etc/services file, you would run:

Terminal
cp /etc/services services
gzip -v services
output
services:	 58.4% -- replaced with services.gz

The file shrank by 58.4%, and gzip replaced services with services.gz.

Compress 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:

Terminal
gzip file1 file2 file3

The 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:

Terminal
gzip -r directory

gzip will recursively traverse through the whole directory structure and compress all the files in the directory and its subdirectories.

Each file is compressed on its own and replaced with a .gz version. The directory is not packed into a single file, as you can see with find :

Terminal
find directory
output
directory
directory/file1.gz
directory/subdir
directory/subdir/file2.gz

Compress a directory into a single file

gzip cannot pack a directory into one file. If you need a single compressed archive, use tar with the -z option, which passes the archive through gzip:

Terminal
tar -czf directory.tar.gz directory

The command creates one directory.tar.gz file and leaves the original directory in place. For more examples, see our guide on creating tar.gz files .

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:

Terminal
gzip -9 filename

Compression is a CPU-intensive task; the higher the compression level, the longer the process takes.

Higher levels do not always mean much smaller files. When we compressed the 1.6 MB /usr/bin/bash binary, -1 produced a 771 KB file, the default level produced 720 KB, and -9 produced 718 KB. For most files, the default level is a good trade-off between speed and size.

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:

Terminal
mysqldump database_name | gzip -c > database_name.sql.gz

The output of the mysqldump command will be input for gzip.

Decompressing Files with gzip

To decompress a .gz file , use the -d option:

Terminal
gzip -d filename.gz

The command creates filename and removes the compressed file, filename.gz.

Another command you can use to decompress a Gzip file is gunzip . It works the same as gzip -d:

Terminal
gunzip filename.gz

You might find it easier to remember gunzip than gzip -d.

Keep the compressed file

The -k option also works when decompressing. In this case, the input is the compressed file, and gzip keeps it in place:

Terminal
gzip -dk filename.gz

Decompress multiple files

To decompress multiple files at once, pass the filenames to the gzip command as arguments:

Terminal
gzip -d file1.gz file2.gz file3.gz

Decompress all files in a directory

When used with -d and -r options, gzip decompresses all files in a given directory recursively:

Terminal
gzip -dr directory

List Compressed File Information

When used with the -l option, the gzip command shows statistics about the given compressed files:

Terminal
gzip -l services.gz

The output will include the compressed and uncompressed size, the compression ratio, and the uncompressed file name:

output
         compressed        uncompressed  ratio uncompressed_name
               5432               12990  58.4% services

In the output above, the 12,990-byte file was compressed to 5,432 bytes.

To get more information, add the -v option:

Terminal
gzip -lv services.gz
output
method  crc     date  time           compressed        uncompressed  ratio uncompressed_name
defla f9468f4f Oct 20 19:23                5432               12990  58.4% services

The extra columns show the compression method (defla for Deflate), the CRC checksum, and the modification time of the original file.

Test a Compressed File

To verify a .gz file without extracting it, use the -t option:

Terminal
gzip -t services.gz

If the file is valid, the command prints nothing and exits with status 0. If you want a confirmation message, add the -v option:

Terminal
gzip -tv services.gz
output
services.gz:	 OK

A damaged or truncated file produces an error, and the exit status is 1:

output
gzip: archive.gz: unexpected end of file

You can use the exit status to check archives in scripts.

View the Contents of a Compressed File

To view the contents of a .gz file without decompressing it, use zcat:

Terminal
zcat filename.gz

zcat 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:

Terminal
zcat access.log.gz | grep 'ERROR'

The zgrep command does the same in one step:

Terminal
zgrep 'ERROR' access.log.gz

gzip -dc is equivalent to zcat. The -d option decompresses, and -c writes the result to standard output instead of a file:

Terminal
gzip -dc filename.gz

Troubleshooting

gzip: filename.gz already exists; do you wish to overwrite (y or n)?
A compressed file with the same name is already in the directory. Answer y to replace it or n to keep it. If you want to overwrite without the prompt, add the -f option. When there is no terminal, such as in a cron job, gzip skips the file and prints not overwritten.

gzip: filename.gz already has .gz suffix -- unchanged
The file is already compressed, and gzip does not compress it again. To decompress it, use gzip -d.

gzip: filename: Too many levels of symbolic links
The file is a symbolic link, and gzip does not follow links by default. Compress the target file instead. With the -f option, gzip compresses the target’s content into filename.gz and removes the link. The target file is not changed.

gzip: compressed data not written to a terminal. Use -f to force compression.
gzip is reading from standard input, and the output would go to the terminal. You need to redirect the output to a file, for example command | gzip > output.gz.

Quick Reference

For a printable quick reference, see the gzip cheatsheet .

CommandDescription
gzip filenameCompress a file (replaces original)
gzip -k filenameCompress and keep the original file
gzip -d filename.gzDecompress a file
gunzip filename.gzDecompress a file (alias for gzip -d)
gzip -r directoryCompress all files in a directory recursively
tar -czf directory.tar.gz directoryCompress a directory into a single archive
gzip -9 filenameMaximum compression
gzip -1 filenameFastest compression
gzip -l filename.gzShow compression statistics
gzip -t filename.gzTest integrity of a compressed file
zcat filename.gzView compressed file contents without extracting
gzip -dc filename.gzDecompress to standard output, keep the .gz file

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 .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

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

View author page