Managing Disk Space   «Prev  Next»
Lesson 3Compressing and uncompressing files
ObjectiveUse compress and uncompress to manage file sizes.

Compressing and uncompressing Files in Unix

Compressing Files

File compression is a way of packing a file's contents more efficiently, so that the file takes up less space. To compress files, enter the compress command and specify the file (or files) you want to act on, like this:
% compress files

This command also renames each file by adding a .Z extension[1] to the name. The compress command accepts a –v option, which is short for “verbose.” Use –v to display a status report that includes the original file name, the new file name, and the amount of compression that occurred. You can compress binary files or text files, but the result is always a binary file. This means you can’t read compressed files with commands such as cat or more.

Restoring Files

To restore a compressed file to its original state, use the uncompress command:
% uncompress file.Z. You can omit the .Z extension from the file name. The uncompress command works either way. The following SlideShow shows some ways you can use compress and uncompress.

Compress Uncompress 1
1) Compress Uncompress 1
Compress Uncompress 2
2) Compress Uncompress 2
Compress Uncompress 3
3) Compress Uncompress 3
Compress Uncompress 4
4) Compress Uncompress 4

How do I Use the commands compress and uncompress to manage file sizes in Unix

To compress a file in Unix, you can use the "compress" command followed by the name of the file you want to compress. For example, to compress a file named "file.txt", you would use the command:
compress file.txt

This will create a new file called "file.txt.Z" which is the compressed version of the original file.
To uncompress a file, you can use the "uncompress" command followed by the name of the compressed file. For example, to uncompress the file "file.txt.Z", you would use the command:
uncompress file.txt.Z

This will create a new file called "file.txt" which is the uncompressed version of the original file. Alternatively you can use the gzip command to compress and gunzip command to uncompress files with the following command :
gzip file.txt
and
gunzip file.txt.gz

Both compress and gzip are file compression utilities, however gzip is more commonly used.

In the next lesson, you will learn to view compressed files with zcat.
[1].Z extension: When you run the compress command, the resulting files are renamed to end with a .Z extension