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

Compressing and uncompressing Files in Unix

In modern Unix-based systems, `compress` and `uncompress` are still available but are largely considered outdated and less commonly used compared to newer tools like `gzip`, `bzip2`, and `xz`.
Key Points:
  • `compress` and `uncompress`** were among the earliest file compression tools in Unix, using the Lempel-Ziv Welch (LZW) algorithm.
  • Drawbacks: They don't compress files as efficiently as modern alternatives like `gzip` (which uses the DEFLATE algorithm) or `xz` (which is more efficient in terms of compression ratio and speed).
  • Compatibility: These tools may still be included in some Unix distributions or available for installation, mainly for backward compatibility.

If you're managing files in a modern Unix system, you're more likely to encounter or prefer using tools like `gzip`, `bzip2`, `xz`, or even `zip` depending on your needs for compression ratio, speed, or compatibility.

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 series of images shows some ways you can use compress and uncompress.


A Telnet session on a terminal interface with a blue background and white text. Here's the text displayed in the terminal:
1) A Telnet session on a terminal interface with a blue background and white text. Here's the text displayed in the terminal:
Connect Edit Terminal Help
danielg % du -ak
1 ./index.html
136 ./doc_plan.html
138
danielg %

Analysis of the Displayed Terminal Session:
  • Telnet session: The terminal title indicates a Telnet connection to a server named "rainforest."
  • Commands and Output:
    • The command `du -ak` has been executed, which is used to display disk usage for all files in the current directory in kilobytes.
    • The output lists two files: `index.html` and `doc_plan.html`, with sizes of 1 KB and 136 KB, respectively.
    • A total size of 138 KB is indicated below the individual file sizes, suggesting the combined disk usage of the directory.
  • User: The prompt "danielg %" indicates the session is being run by a user named "danielg."

This interface is typical of older Unix and Linux systems, indicating a focus on system administration or development tasks. The style of the interface suggests it may be from a retro or legacy system, often seen in environments that maintain older infrastructure for compatibility or specific needs.


displays another Telnet session on a terminal interface with the following text:
2)
Connect Edit Terminal Help
danielg % compress -v doc_plan.html
doc_plan.html: Compression: 48.98% — replaced with doc_plan.html.Z
danielg %

Analysis of the Displayed Terminal Session:
  • Telnet session: Like the previous image, this shows a session titled "rainforest," indicating the server or system being accessed.
  • Command and Output:
    • The `compress -v doc_plan.html` command has been executed. This command compresses the file `doc_plan.html`, and the `-v` (verbose) option provides detailed output about the compression.
    • The output shows a compression ratio of 48.98%, indicating that the original file size has been reduced by nearly half.
    • The original file is replaced with a compressed version named `doc_plan.html.Z`, following the convention of appending `.Z` to the file name for compressed files using the Unix `compress` utility.
  • User: The session is managed by a user named "danielg", consistent with the previous session.

This session highlights a typical system administration task involving file compression to save disk space, often necessary in environments where storage optimization is crucial. The style and utility usage suggest an interaction with a Unix-like operating system, maintaining the focus on system-level operations and management.
Telnet 3
3) Telnet 3

Telnet 4
4) Telnet 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

SEMrush Software 3 SEMrush Banner 3