Linux gzip Command: Compress a File in gzip Format
gzip compresses file data into the .gz format. It does not combine multiple files into one archive and usually converts the original file into a compressed file.
What is the gzip command?
When you specify the file to compress, usually a file.gz is created and the original file is removed. To keep the original, use -k or send it to standard output with -c and save it in a separate file. To compress while preserving directory structure, it is appropriate to first bundle with tar.
Basic syntax
gzip [options] [file...]
Installed implementations and options may vary depending on the distribution. Check the current system's description with man gzip.
Examples
Compress while keeping the original
Use -k to keep the original and create a new .gz file.
gzip -k report.log
Save the compression result under a different name
-c sends the compressed data to standard output without changing the original. Make sure the output file does not already exist.
gzip -c report.log > report-copy.log.gz
Compressed File Integrity Check
Checks for gzip stream errors without extracting the file contents.
gzip -t report.log.gz
Check if the original file remains after compression
The gzip file command by default replaces the original with a .gz file. If you want to keep the original as well, use GNU gzip's -k option.
printf 'hello\n' > note.txt gzip -k note.txt ls note.txt*
Example output:
note.txt note.txt.gz
Compression success itself usually does not display anything on the screen. You can check the structure of a compressed file with gzip -t note.txt.gz, but it does not guarantee that it is identical to the original content. If you want to combine multiple files into one, use tar first and then apply gzip.
Main Options and Format
| Options/Format | Description |
|---|---|
-k |
Keeps the input file after compression. |
-c |
Sends the result to standard output. |
-t |
Verifies the integrity of compressed data. |
-1 ... -9 |
Chooses a level from speed priority to compression ratio priority. |
-v |
Displays information about processing, such as compression ratio. |
Precautions when using
The default execution replaces the original file, so use -k when you need to keep the original. Even if multiple files are given as arguments, they will not be combined into a single .gz. gzip compression is not encryption and should not be used for protecting confidential materials.
Frequently Asked Questions
Can you use gzip to make an entire directory into a single file?
gzip itself is not an archiver that contains directory structures. Use it together with tar like tar -czf backup.tar.gz directory/.
Official Documentation
You can check the exact behavior of options and implementation differences in the official documentation for gzip.









