100 Useful Command-Line Utilities

by Oliver; 2014

44. gzip, gunzip, bzip2, bunzip2

It's a good practice to conserve disk space whenever possible and unix has many file compression utilities for this purpose, gzip and bzip2 among them. File compression is a whole science unto itself which you can read about here.

Zip a file:
$ gzip file
(The original file disappears and only the .gz file remains)

Unzip:
$ gunzip file.gz
Bunzip:
$ bunzip2 file.bz2 
You can pipe into these commands!
$ cat file.gz | gunzip | head
$ cat file | awk '{print $1"\t"100}' | gzip > file2.gz
The first preserves the zipped file while allowing you to look at it, while the second illustrates how one may save space by creating a zipped file right off the bat (in this case, with some random awk).

cat a file without a unzipping it:
$ zcat file.gz
less a file without a unzipping it:
$ zless file.gz
To emphasize the point again, if you're dealing with large data files, you should always compress them to save space.

<PREV   NEXT>