Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux gzip Command

Linux gzip Command

Linux Command Reference Linux Command Reference

The Linux gzip command is used to compress files.

gzip is a widely used compression program; after a file is compressed by it, the extension .gz is appended to its name.

gzip uses the DEFLATE compression algorithm and is usually faster than bzip2 but with a slightly lower compression ratio.

Syntax



gzip [options] [file...]

options parameter:

Example

Compress a file

gzip example.txt

This command compresses example.txt into example.txt.gz and deletes the original example.txt.

** Keep the original file**

If you want to keep the original file after compression, you can use the -k option:



gzip -k example.txt

This command keeps the original example.txt file and generates example.txt.gz.

** Decompress a file**

To decompress a .gz file, you can use the -d option or use gunzip directly:



gzip -d example.txt.gz

or



gunzip example.txt.gz

This will decompress example.txt.gz back into the original example.txt file.

** Recursively compress a directory**

You can use the -r option to recursively compress an entire directory:



gzip -r directory/

This command compresses all files under the directory directory and preserves the directory structure.

** Display compressed file information**

Use the -l option to view detailed information about .gz files:



gzip -l example.txt.gz

This command displays information such as the original size, compressed size, and compression ratio of the file.

** Test compressed files**

Use the -t option to test the integrity of a compressed file:



gzip -t example.txt.gz

If the file is intact and undamaged, the command produces no output; otherwise, it reports an error.

gzip is mainly used to compress a single file. If you need to compress multiple files or an entire directory, you typically first archive with tar and then compress with gzip. For example:

tar -cvzf archive.tar.gz directory/

Linux Command Reference Linux Command Reference