Compressed files use less disk space and download faster than large, uncompressed files. In Red Hat Linux you can compress files with the compression tools gzip, bzip2, or zip.
The bzip2 compression tool is recommended because it provides the most compression and is found on most UNIX-like operating systems. The gzip compression tool can also be found on most UNIX-like operating systems. If you need to transfer files between Linux and other operating system such as MS Windows, you should use zip because it is more compatible with the compression utilities on Windows.
| Compression Tool |
File Extension |
Uncompression Tool |
| gzip |
.gz |
gunzip |
| bzip2 |
.bz2 |
bunzip2 |
| zip |
.zip |
unzip |
By convention, files compressed with gzip are given the extension .gz, files compressed with bzip2 are given the extension .bz2, and files compressed with zip are given the extension .zip. Files compressed with gzip are uncompressed with gunzip, files compressed with bzip2 are uncompressed with bunzip2, and files compressed with zip are uncompressed with unzip.
Bzip2 and Bunzip2
To use bzip2 to compress a file,The file will be compressed and saved as filename.bz2. type the following command at a shell prompt:
bzip2 filename
To expand the compressed file , The filename.bz2 is deleted and replaced with filename.type the following command:
bunzip2 filename.bz2
You can use bzip2 to compress multiple files and directories at the same time by listing them with a space between each one:
bzip2 filename.bz2 file1 file2 file3 /usr/work/school
The above command compresses file1, file2, file3, and the contents of the /usr/work/school directory (assuming this directory exists) and places them in a file named filename.bz2.
Gzip and Gunzip
To use gzip to compress a file, type the following command at a shell prompt:
gzip filename
The file will be compressed and saved as filename.gz. To expand the compressed file, type the following command:
gunzip filename.gz
The filename.gz is deleted and replaced with filename. You can use gzip to compress multiple files and directories at the same time by listing them with a space between each one:
gzip -r filename.gz file1 file2 file3 /usr/work/school
The above command compresses file1, file2, file3, and the contents of the /usr/work/school directory (assuming this directory exists) and places them in a file named filename.gz.
Zip and Unzip
To compress a file with zip, type the following command:
zip -r filename.zip
filesdir In this example, filename.zip represents the file you are creating and filesdir represents the directory you want to put in the new zip file. The -r option specifies that you want to include all files contained in the filesdir directory recursively. To extract the contents of a zip file, type the following command:
unzip filename.zip
You can use zip to compress multiple files and directories at the same time by listing them with a space between each one:
zip -r filename.zip file1 file2 file3 /usr/work/school
The above command compresses file1, file2, file3, and the contents of the /usr/work/school directory (assuming this directory exists) and places them in a file named filename.zip.