gzip will compress a file and gunzip will decompress it back to the original:
- Compress a file with gzip:
$ gzip filename
$ ls
filename.gz
- Extract a gzip compressed file:
$ gunzip filename.gz
$ ls
filename
- In order to list the properties of a compressed file, use the following command:
$ gzip -l test.txt.gz
compressed uncompressed ratio uncompressed_name
35 6 -33.3% test.txt
- The gzip command can read a file from stdin and write a compressed file to stdout.
Read data from stdin and output the compressed data to stdout:
$ cat file | gzip -c > file.gz
The -c option is used to specify output to stdout.
The gzip -c option works well with cpio:
$ ls * | cpio -o | gzip -c > cpiooutput.gz
$ zcat cpiooutput.gz | cpio -it
- We can specify the compression level for gzip using --fast or the --best option to provide low and high compression ratios, respectively.