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, its name will have an additional .gz.gzextension.
gzip uses the DEFLATE compression algorithm, which is usuallybzip2faster, but with a slightly lower compression ratio.
Syntax
gzip [options] [file...]
file...: the file to be compressed.gzipwill compress the specified file and generate a.gzcompressed file with the .gz suffix, while deleting the original file.
Option parameters:
-d: decompress.gzthe file. Equivalent to using thegunzipcommand.-k: keep the original file without deleting it.-r: recursively compress all files in the directory.-v: display the detailed compression or decompression process.-l: display detailed information about the compressed file, such as compression ratio, original size, etc.-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.
Examples
Compress a file
gzip example.txt
This command compresses example.txt into example.txt.gz and deletes the original file 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 directly use gunzip:
gzip -d example.txt.gz
or
gunzip example.txt.gz
This will decompress example.txt.gz 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 in the 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 file's original size, compressed size, and compression ratio.
Test a compressed file
Use the -t option to test the integrity of the compressed file:
gzip -t example.txt.gz
If the file is intact and undamaged, the command will produce no output; otherwise, it will report an error.
gzip is mainly used to compress a single file. If you need to compress multiple files or an entire directory, you usually use tar to archive first, then gzip to compress. For example:
tar -cvzf archive.tar.gz directory/Other extensions
Linux Command Reference