gzip Overview
`gzip` is a tool optimized for compressing a single file. To compress multiple files, it is common practice to first bundle them with the `tar` command and then compress them with `gzip`. Files created this way have a `.tar.gz` or `.tgz` extension. A key point to remember is that `gzip`'s default behavior is to delete the original file and leave the compressed one, so caution is advised.
Main Roles of gzip
The `gzip` command is primarily used for the following purposes:
Main Application Areas
- Saving Disk Space: Compresses large log files and backup files to use storage space more efficiently.
- Improving Network Transfer Efficiency: Reduces bandwidth usage and shortens transfer time by sending compressed files.
- Archiving Assistance: Used with `tar` to bundle and compress multiple files and directories.
Key gzip Command Options
The `gzip` command allows you to control compression ratio, whether to preserve the original file, and perform recursive processing through various options during compression and decompression.
1. Basic Compression and Decompression
2. Compression Level and Recursive Processing
3. Other Information and Control
Generated command:
Try combining the commands.
Description:
`gzip` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Explore these various examples of the `gzip` command to learn how to efficiently compress and decompress files and manage disk space.
Compress a single file
gzip mylog.txt
Compresses the file `mylog.txt` to create `mylog.txt.gz`, deleting the original file.
Decompress a compressed file (same as gunzip)
gzip -d mylog.txt.gz
Decompresses the file `mylog.txt.gz` to create `mylog.txt`, deleting the compressed file.
Keep the original file during compression
gzip -k report.log
Compresses `report.log` to create `report.log.gz`, but keeps the original file `report.log`.
Compress with the best compression ratio
gzip -9 big_data.csv
Compresses `big_data.csv` with the highest compression ratio to minimize file size. This may take longer.
Recursively compress all files in a directory
gzip -r my_docs/
Compresses all files (including those in subdirectories) within the `my_docs` directory with a `.gz` extension.
Check information of a compressed file
gzip -l backup.sql.gz
Checks the pre/post-compression size and compression ratio of `backup.sql.gz`.
Use tar and gzip together to compress an archive
tar -cf - my_project | gzip > my_project.tar.gz
Bundles the `my_project` directory with `tar` and then compresses it with `gzip` to create a `my_project.tar.gz` file. (While `tar -czvf` is commonly used, this example separates the commands for conceptual clarity.)