Cheatsheet & Examples: gzip

I need a cheatsheet for the gzip
command.
Compress a file
Example Usage:
gzip file.txt
What it does:
Compresses the specified file using the gzip algorithm, replacing the original file with a compressed version. The compressed file will have the .gz
extension.
Command-line Arguments Explained:
- file.txt: The path to the file to be compressed.
Compress multiple files
Example Usage:
gzip file1.txt file2.txt
What it does:
Compresses multiple files in one command, creating .gz
versions of each.
Command-line Arguments Explained:
- file1.txt: The first file to be compressed.
- file2.txt: The second file to be compressed.
Compress and keep the original file
Example Usage:
gzip -k file.txt
What it does:
Compresses the file while retaining the original (uncompressed) version. The compressed file will have the .gz
extension.
Command-line Arguments Explained:
- -k: Keeps the original file after compression. Without this option, the original is deleted.
Compress with a specific compression level
Example Usage:
gzip -9 file.txt
What it does: Compresses the file using the maximum compression level (9), which may take longer but produce smaller files.
Command-line Arguments Explained:
- -9: Sets the compression level to 9 (the highest, with 1 being the fastest and 9 the most compressed). Available levels: 1-9.
Decompress a file
Example Usage:
gzip -d file.gz
What it does:
Decompresses the specified .gz
file, restoring the original content. The compressed file is replaced with the decompressed version.
Command-line Arguments Explained:
- -d: Decompresses the file. This is equivalent to using
gunzip
but is a valid option forgzip
.
Force compression (override existing files)
Example Usage:
gzip -f file.txt
What it does:
Forces compression of the file, overwriting any existing .gz
file with the same name if it exists.
Command-line Arguments Explained:
- -f: Forces compression, even if the output file already exists. Useful to avoid prompts for confirmation.
Compress to standard output
Example Usage:
gzip -c file.txt > file.gz
What it does: Compresses the file and writes the output to standard output, which can be redirected to a file or another program.
Command-line Arguments Explained:
- -c: Outputs the compressed data to standard output instead of modifying the input file. Requires redirection to save the result.
List information about compressed files
Example Usage:
gzip -l file.gz
What it does: Displays details about the compressed file, such as original size, compressed size, and compression ratio.
Command-line Arguments Explained:
- -l: Lists information about the compressed file. If no file is specified, it lists all
.gz
files in the current directory.
Recursively compress files in a directory
Example Usage:
gzip -r /path/to/dir
What it does: Recursively compresses all files in the specified directory and its subdirectories.
Command-line Arguments Explained:
- -r: Recursively processes files in the directory. This is useful for batch compression of directories.
Compress without saving the filename
Example Usage:
gzip -n file.txt
What it does: Compresses the file and omits saving the original filename in the compressed data, which can help in reducing file size slightly.
Command-line Arguments Explained:
- -n: Omits the original filename from the compressed file. This is often used with
.gz
files where the filename is not needed.
Subscribe to my newsletter
Read articles from Hong directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Hong
Hong
I am a developer from Malaysia. I work with PHP most of the time, recently I fell in love with Go. When I am not working, I will be ballroom dancing :-)