How to Use gunzip Command in Linux: Syntax and Examples
Use the
gunzip command in Linux to decompress files compressed with gzip, typically ending with .gz. Run gunzip filename.gz to replace the compressed file with the decompressed version.Syntax
The basic syntax of the gunzip command is:
gunzip [options] filename.gz
Here, filename.gz is the compressed file you want to decompress. The command removes the .gz extension and replaces the compressed file with the original uncompressed file.
Common options include:
-c: Write output to standard output (screen) instead of replacing the file.-k: Keep the original compressed file after decompressing.-f: Force decompression even if the file has multiple links or is not compressed.
bash
gunzip [options] filename.gz
Example
This example shows how to decompress a file named example.txt.gz using gunzip. The command will remove the compressed file and create example.txt with the original content.
bash
echo "Hello, world!" > example.txt
gzip example.txt
ls example.txt.gz
# Now decompress
gunzip example.txt.gz
ls example.txtOutput
example.txt.gz
example.txt
Common Pitfalls
Some common mistakes when using gunzip include:
- Trying to decompress a file that is not compressed with gzip, which will cause an error.
- Expecting
gunzipto keep the original compressed file by default; it deletes it unless you use-k. - Not having write permission in the directory, causing decompression to fail.
Example of wrong and right usage:
bash
# Wrong: decompressing a non-gzip file # gunzip file.txt # Right: decompressing a gzip file and keeping original # gunzip -k file.txt.gz
Quick Reference
| Option | Description |
|---|---|
| -c | Write output to standard output, do not delete original file |
| -k | Keep the original compressed file after decompressing |
| -f | Force decompression even if file has multiple links or is not compressed |
| -l | List compressed file contents without decompressing |
| -t | Test compressed file integrity |
Key Takeaways
Use
gunzip filename.gz to decompress and replace the compressed file.Add
-k option to keep the original compressed file after decompression.Use
-c to output decompressed data to the screen or another command.Ensure the file is actually gzip compressed to avoid errors.
Check permissions to allow writing decompressed files in the directory.