gzip command is used to reduce the size of given files. This command is so common that you will most likely find it already installed on your Linux distro.
gzip only compresses regular files. Directories, symbolic links etc are not it's food. Files compressed with
gzip are often called gzipped files.
gzip also comes with the
zless commands for uncompressing and viewing gzipped files.
Let's start gzipping!
To compress one or more files we can simply do like below:
gzip file1 file2 file3
It will compress each file and replace them with compressed versions of them which have a
file1.gz file2.gz file3.gz
gzip can also be used via standard input and output:
ls -l /bin | gzip > foo.txt.gz
With the command
gunzip we can uncompress them like below:
gunzip file1.gz file2.gz file3.gz
It will replace each compressed file with it's uncompressed version. So you will get the following files:
file1 file2 file3
We can use the
-r option to recursively compress each file in a directory:
gzip -r someDir
For a directory containing any gzipped files, we can use
-r to uncompress all of them recursively with a single command:
gunzip -r someDir
-c option can be used by both
gunzip to write output to standard output and keep the original files:
gzip -c some-file > some-file.gz gunzip -c some-file.gz > some-file-copy
When a text file is compressed, it's sometimes handy to view the text without uncompressing and writing it to disk.
To view the contents of a compressed file, there are several ways.
We can simply use the
-c option and pipe the output to
less to view it:
gunzip -c file1.gz | less
zcat can be used like
gzip compressed files. So we can be little more succinct:
zcat file1.gz | less
zless allows us view gzipped files in the most clean way:
That's for this article. Hope you have learned something useful. For digging deeper see the man page
man gzip. Happy gzipping!