Linux/Unix – Compressing Files
File compression is useful for storing or transferring large files. When you compress a file, you shrink it and save disk space. File compression uses an algorithm to change the data in the file; to use the data in compressed files; you must first uncompress it to restore the original data (and original file size). The following recipes explain how to compress and uncompress files.
Compressing a File
Use the gzip (“GNU zip”) tool to compress files. It takes as an argument the name of the file or files to be compressed; it writes a compressed version of the specified files, appends a `.gz’ extension to their file names, and then deletes the original files. To compress the file `war−and−peace’, type: $ gzip war−and−peace
This command compresses the file `war−and−peace’, putting it in a new file named `war−and−peace.gz’; gzip then deletes the original file, `war−and−peace’.
Decompressing a File
To access the contents of a compressed file, use gunzip to decompress (or “uncompress”) it.
Like gzip, gunzip takes as an argument the name of the file or files to work on. It expands the specified files, writing the output to new files without the `.gz’ extensions, and then deletes the compressed files. To expand the file `war−and−peace.gz’, type: $ gunzip war−and−peace.gz
This command expands the file `war−and−peace.gz’ and puts it in a new file called `war−and−peace’; gunzip then deletes the compressed file, `war−and−peace.gz’.
NOTE: You can view a compressed text file without uncompressing it by using zless. This is useful when you want to view a compressed file but do not want to write changes to it.