Using Gzip to create .gz files

- 3 min read

Compressing files from the terminal doesn’t need to be a magical combination of options. Learn all the basics of Gzip with examples.

Compressing a file with Gzip

Compressing a file can be completed by running:

gzip YOUR-FILE

However, this will remove YOUR-FILE and leave you with YOUR-FILE.gz. If you want to keep your original file—so you’d end up with YOUR-FILE and YOUR-FILE.gz, then you’ll need to use the keep (-k or --keep) option.

gzip -k YOUR-FILE

If you want to compress your file as much as possible, you’ll also want to set your compression level. The options range from 1 to 9.

  • 1 will give you the largest file (least compression), but will finish as quickly as possible.
  • 2-8 will give you a balance between least or most compression, at the cost of speed. If you do not specify a compression level, 6 is the default.
  • 9 will give you the smallest file, but will take the most processing power.
gzip -k1 YOUR-FILE
gzip -k6 YOUR-FILE
gzip -k9 YOUR-FILE
# Without keeping your file
gzip -9 YOUR-FILE 

Compressing stdin with Gzip

Gzip works just as well with stdin as with a file. However, you will need to specify your .gz file to write to—or use the stdout as input for another command with a pipe.

# Write to a file
cat YOUR-FILE | gzip -9 > YOUR-FILE.gz

# Pipe to another command
cat YOUR-FILE | gzip -3 | openssl rsautl -inkey YOUR-KEY.txt -encrypt > YOUR-ENCRYPTED-FILE.bin

Sending Gzip result to stdout

When we compressed from stdin, Gzip automatically set the output to stdout—rather than writing to a file. However, if we want to Gzip a file and send the compressed output to stdout, we’ll need to use the -c option.

gzip -c9 YOUR-FILE > SOME-OTHER-DEST.gz

Note, -c automatically keeps your file, -k is not needed.

Viewing compression information about a file

Once you have a .gz file, you can view compression information about it with the -l option.

# Worst compression
gzip -k1 YOUR-FILE
gzip -l YOUR-FILE.gz
         compressed        uncompressed  ratio uncompressed_name
               4088               27475  85.2% YOUR-FILE

# Best compression
gzip -k9 YOUR-FILE
gzip -l YOUR-FILE.gz
         compressed        uncompressed  ratio uncompressed_name
               3244               27475  88.3% YOUR-FILE

The amount of compression achieved for your file will be dependent upon what you are compressing.

Decompressing

Gzip can also decompress your .gz file with the -d option.

# Keep the .gz file
gzip -kd YOUR-FILE.gz

# Or don't
gzip -d YOUR-FILE.gz

Recompressing a file

If you have an existing .gz file that was compressed with a faster compression (such as -1), we can recompress the file.

First, your file:

gzip -l YOUR-FILE.gz
         compressed        uncompressed  ratio uncompressed_name
               4093               27475  85.2% YOUR-FILE

Recompress your file by decompressing (-d), sending to stdout (-c) piping to the gzip command with a better compression level:

gzip -cd YOUR-FILE.gz | gzip -9 > YOUR-SMALLER-FILE.gz

Check your result (if you want):

gzip -l YOUR-SMALLER-FILE.gz
         compressed        uncompressed  ratio uncompressed_name
               3239               27475  88.3% YOUR-SMALLER-FILE