Open In App

How to compress file in Linux | Compress Command

Linux, renowned for its powerful command-line utilities, provides users with various tools to streamline file management tasks. One such tool, the compress `command`, is specifically designed to compress individual files, reducing their size for efficient storage and transfer. In this comprehensive guide, we’ll explore the ins and outs of the compress `command`, covering its usage, options, and practical examples.

Compress Command in Linux

The compress command in Linux is a straightforward yet effective tool for compressing files using the Lempel-Ziv compression algorithm. While it may not be as feature-rich as some modern compression tools, it serves as a quick and efficient solution for reducing the size of individual files.

Installation of Compress Command in Linux

If we are using the Ubuntu Linux Distribution, then we will use this command to install the compress command.

sudo apt install ncompress

Compress command installation

Syntax of compress command in Linux

The basic syntax of the `compress` the command is simple:

compress [options] filename

Here, “filename" is the name of the file you want to compress. The command replaces the original file with a compressed version, appending the “.Z” extension.Linux

Common Options in Compress Command in Linux

Examples of the ‘compress’ Command:

1. Compressing a Single File Using Compress Command in Linux

To compress a single file using the `compress` command, execute the following command in the terminal:

compress example.txt

This command compresses the file named `example.txt` and generates a compressed version with the extension `.Z`, resulting in a new file named `example.txt.Z` in the same directory. The compression process aims to reduce the size of the file for more efficient storage and transfer.

We used `ls` command to verify the successful compression of our file.

2. Compress Multiple Files Using Compress Command in Linux

The `compress` command supports the compression of multiple files simultaneously. For instance:

compress file1.txt file2.txt file3.txt

This command compresses three files—`file1.txt`, `file2.txt`, and `file3.txt`. The output includes three compressed files: `file1.txt.Z`, `file2.txt.Z`, and `file3.txt.Z`. This capability enhances efficiency when dealing with batches of files that require compression.

3. Decompressing a File Using Compress Command in Linux

To decompress a file that was previously compressed using the `compress` command, employ the `uncompress` command:

uncompress example.txt.Z

Decompressing a File

This command decompresses the file `example.txt.Z` and restores it to its original state as `example.txt`. The decompression process reverses the compression, allowing you to retrieve the original file for further use or modification.

These examples provide a detailed walkthrough of compressing single and multiple files using the `compress` command, as well as the subsequent decompression process using `uncompress`. Understanding these operations enhances your ability to manage files effectively on a Linux system.

4. Redirecting Compressed Output Using Compress Command in Linux

The `-c` option allows you to redirect the compressed output to another file or command:

compress -c filename > compressed_output.Z

This is useful when you want to keep the original file intact and create a separate compressed version.

For Example we want to to compress file name “example.txt” and want to redirect the compressed output to “compressed_output..Z”

Redirecting compressed Output

5. `-v` Option in compress command in Linux

It is used to print the percentage reduction of each file. This option is ignored if the -c option is also used. Compress example.xls and rename that file to example.xls.Z with a percentage reduction of 70.41%.

compress -v example.xls

-v option

6. `-r` Option in compress command in Linux

This will compress all the files in the given directory and sub-directories recursively. It is helpful to combine the -r option with -v to see exactly what the command is doing.


compress -rv abc

This will compress all the files within abc.

7. `-f` Option in compress command in Linux

This will compress files without any guarantee of size reduction, meaning it will compress files even if the file size is not reduced.

compress -f asc.txt

This will convert asc.txt into asc.txt.Z but size reduction is not assured.

Frequently Asked Question in Compress Command

1. Can I compress multiple files simultaneously using the compress command in Linux?

Yes, the compress command supports the compression of multiple files in one go. For example, you can use:

compress file1.txt file2.txt file3.txt

This will create compressed versions for each file, such as file1.txt.Z, file2.txt.Z, and file3.txt.Z.

2. How can I check the percentage reduction achieved after compressing a file with the compress command?

Utilize the -v option with the compress command. For instance:

compress -v example.txt

This will display the percentage reduction achieved during the compression process.

3. Is it possible to compress a file without modifying the original and redirecting the output to another file?

Yes, you can achieve this using the -c option. For example:

compress -c example.txt > compressed_output.Z

This command writes the compressed content of example.txt to compressed_output.Z without altering the original file.

4. Can the compress command be used to compress directories and subdirectories recursively?

No, the compress command is designed for compressing individual files. To compress directories and subdirectories recursively, consider using other tools like tar in conjunction with compress.

5. Does the compress command guarantee a reduction in file size, or can it be used even if the file size may not decrease?

The `-f` option in the `compress` command allows compression without any guarantee of size reduction. For instance:

compress -f large_file.txt

This command will compress large_file.txt into large_file.txt.Z even if the size reduction is not assured.


In this article we discussed the ‘compress’ command which is a reliable and straightforward way to reduce file sizes using the Lempel-Ziv coding algorithm. With its various options, users can efficiently compress and decompress files, saving disk space and enhancing data transfer efficiency. By mastering this command, Linux users can streamline their file management tasks and optimize their overall computing experience.

Article Tags :