Linux compress Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux compress command, which is used to compress and decompress files. The compress command is a classic compression utility that uses the Lempel-Ziv-Welch (LZW) compression algorithm. We will start by introducing the compress command, then demonstrate how to compress and decompress files using it, and finally explore some advanced options of the command.

The lab covers the following steps:

  1. Introduce the Linux compress command
  2. Compress and decompress files using the compress command
  3. Explore advanced options of the compress command

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") subgraph Lab Skills linux/echo -.-> lab-422614{{"`Linux compress Command with Practical Examples`"}} linux/ls -.-> lab-422614{{"`Linux compress Command with Practical Examples`"}} end

Introduce the Linux compress Command

In this step, we will introduce the Linux compress command, which is used to compress and decompress files. The compress command is a classic compression utility that uses the Lempel-Ziv-Welch (LZW) compression algorithm.

To get started, let's first check the version of the compress command installed on our system:

$ compress --version
compress 4.3.1

The compress command is typically used to compress individual files or directories. The compressed files are given the extension .Z. For example, if we have a file named example.txt, the compressed version will be example.txt.Z.

To compress a file using the compress command, simply run:

$ compress example.txt

This will create the compressed file example.txt.Z.

To decompress a file, you can use the uncompress command:

$ uncompress example.txt.Z

This will create the original example.txt file.

Example output:

$ ls
example.txt  example.txt.Z
$ uncompress example.txt.Z
$ ls
example.txt

In the next step, we will explore more advanced options of the compress command.

Compress and Decompress Files Using compress Command

In this step, we will learn how to use the compress command to compress and decompress files.

First, let's create a sample text file to work with:

$ echo "This is a sample text file." > example.txt

Now, let's compress the example.txt file using the compress command:

$ compress example.txt

This will create the compressed file example.txt.Z.

To see the difference in file size, let's check the file sizes:

$ ls -l
-rw-r--r-- 1 labex labex 27 Apr 17 08:36 example.txt
-rw-r--r-- 1 labex labex 23 Apr 17 08:36 example.txt.Z

As you can see, the compressed file example.txt.Z is smaller in size compared to the original example.txt file.

Now, let's decompress the example.txt.Z file using the uncompress command:

$ uncompress example.txt.Z

This will create the original example.txt file.

Example output:

$ ls
example.txt  example.txt.Z
$ uncompress example.txt.Z
$ ls
example.txt

In the next step, we will explore some advanced options of the compress command.

Explore Advanced Options of compress Command

In this step, we will explore some advanced options of the compress command.

The compress command has several options that allow you to customize the compression process. Some of the commonly used options are:

  • -c: Write output to standard output instead of replacing the original file.
  • -f: Force compression, even if the compressed file is larger than the original.
  • -v: Verbose mode, display the compression ratio.
  • -b: Set the block size (default is 16K).

Let's try out some of these options:

Compress a file and write the output to standard output:

$ compress -c example.txt
This is a sample text file.

Force compression even if the compressed file is larger:

$ compress -f example.txt
$ ls -l
-rw-r--r-- 1 labex labex 27 Apr 17 08:36 example.txt
-rw-r--r-- 1 labex labex 32 Apr 17 08:36 example.txt.Z

Display the compression ratio in verbose mode:

$ compress -v example.txt
example.txt: 85.19% -- replaced with example.txt.Z

Set the block size to 8K:

$ compress -b 8 example.txt
$ ls -l
-rw-r--r-- 1 labex labex 27 Apr 17 08:36 example.txt
-rw-r--r-- 1 labex labex 28 Apr 17 08:36 example.txt.Z

These are just a few examples of the advanced options available with the compress command. Experiment with different options to find the ones that work best for your needs.

Summary

In this lab, we first introduced the Linux compress command, which is used to compress and decompress files using the Lempel-Ziv-Welch (LZW) compression algorithm. We learned how to check the version of the compress command and how to use it to compress and decompress files. We then explored more advanced options of the compress command, such as compressing and decompressing files with different file extensions and using various compression levels.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like