Session and utilities
The gzip command
Compresses a file with the gzip algorithm and adds the .gz extension. It is the de facto standard on Linux for compressing single files. It works on one file at a time: to pack whole folders, combine it with tar.
Most useful options
-k, --keepKeeps the original. Without -k, gzip replaces it with the .gz-d, --decompressDecompresses. It is the same as gunzip-c, --stdoutWrites the result to standard output and leaves the file alone-l, --listShows the compressed size, the original size and the percentage saved-t, --testChecks the integrity of the .gz without extracting it-1 ... -9Compression level: -1 is the fastest, -9 the smallest. Default is -6Copy-ready examples
$ gzip -k informe.txtcreates informe.txt.gz and keeps the original.$ gzip -l registro.log.gzshows how much compression saves without decompressing. Sizes are illustrative.Example output
compressed uncompressed ratio uncompressed_name
1213 9860 87.7% registro.log$ gzip -dk informe.txt.gzdecompresses and keeps the .gz.$ gzip -c datos.csv > datos.csv.gzthe same as -k, but with a redirect. Useful when you want to choose the output name.You can try them in the browser terminal: it does not touch your computer.
Common errors
| Message | Cause | Fix |
|---|---|---|
| gzip: noexiste: No such file or directory | The file does not exist or the name is misspelled. | List the folder with ls and copy the exact name. |
| gzip: a.txt.gz already exists; not overwritten | The target .gz already exists, and gzip does not overwrite it silently. | Rename or delete the old .gz, or use -f if you really want to replace it. |
| gzip: dir is a directory -- ignored | gzip does not compress folders. It skips the directory. | Pack the folder with tar -czf dir.tar.gz dir. |
| gzip: n.txt: not in gzip format | You tried to decompress or test with -t a file that is not gzip. | Check the type with file. If the download is incomplete, download it again. |
Typical mistakes
- Without -k, gzip deletes the original when compressing. Use -k if you still need it.
Differences from similar commands
Security
gzip encrypts nothing: anyone with gunzip can read the content. If the file holds passwords or personal data, encrypt it with gpg or openssl before storing or sending it.
FAQ
Why can a small file get bigger when compressed?
Because gzip adds a header and needs repeated data to save space. On tiny files the percentage can be negative.
What is the difference between gzip and tar.gz?
gzip compresses one file. tar.gz first packs several files or folders into a tar and then compresses it.
How do I decompress without deleting the .gz?
With gzip -dk archivo.gz or gunzip -k archivo.gz.
Learn to actually use it
The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.
Start the free course