Linux gzip Command
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...]
file...: the file to compress.gzipcompresses the specified file and generates a.gzcompressed file with the suffix, while deleting the original file.
options parameter:
-d: Decompress.gzfile. Equivalent to usinggunzipcommand.-k: keep the original file, do not delete it.-r: Recursively compress all files in a directory.-v: display the detailed compression or decompression process.-l: Display detailed information about compressed files, such as compression ratio, original size, and so on.-1to-9: specify the compression ratio.-1is the fastest compression, with the lowest compression ratio;-9is the slowest compression, with the highest compression ratio. The default is-6.-t: test the integrity of the compressed file.
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