How to use compress, zip, gzip commands under Linux

File Compression

With the enormous amount of enterprise data that is created and stored, there is a pressing need to conserve disk space and optimize data transfer time. There are various tools, utilities, and commands that are used for file compression. Some of the commonly used commands are:
– The compress command
– The gzip command
– The zip command

The compress Command

When compressing a file, the compress command replaces the original file with a new file that has a .Z extension.

$ compress [ -v ] filename

The ownership and modification time of the original file remain intact, but the content of the file changes. The amount of compression depends on the type of file you compress. Typically, compression reduces a text file by 50 to 60 percent.

Compressing a File: compress Command

The following example shows you how to compress a file called test.tar.

# compress -v test.tar 
test.tar:  -- replaced with test.tar.Z Compression: 96.35%
# ls -lrt test.tar.Z 
-rw-r--r--. 1 root root 373 Dec  7 19:31 test.tar.Z

Here,
– The -v (verbose) option provides information about the percentage of reduction or expansion of each file.
– The compressed file, test.tar.Z, replaces the test.tar file.

Note: When you compress a file that has already been compressed, the file size increases, instead of becoming smaller. Also, when you rename a file that has already been compressed and you run the compress command on it once again, the file size increases, instead of becoming smaller.

Viewing a Compressed File: zcat Command

The zcat command prints the uncompressed form of a compressed file to the standard output.

$ zcat filename

To view the content of the data.txt.Z compressed file, enter the following command:

# zcat data.txt.Z 
northwest       NW      Joel Craig          10
western         WE      Sharon Kelly        40
southwest       SW      Chris Foster        33
northeast       NE      TJ Nichols          67
north           NO      Val Shultz          91
central         CT      Sheri Watson        44
Note: The zcat command interprets the compressed data and displays the content of the file as if it were not compressed.

The zcat filename command is functionally identical to the uncompress -c filename command. For example the to view the content of the compressed file data.txt.Z, we can also use the command shown below.

uncompress -c data.txt.Z

Uncompressing a File: uncompress Command

The uncompress command restores a compressed file to an uncompressed state.

$ uncompress options filename

To uncompress the test.tar.Z file and restore it to the test.tar file, enter the following command:

# uncompress -v test.tar.Z 
test.tar.Z:  -- replaced with test.tar

The -v option displays additional messages about the action being performed.

You can use the uncompress command with the -c option to send the content of a compressed file to the stdout, the screen, without changing the compressed .Z file. Otherwise, you can use the pipe (|) character to send the output of the uncompress command to another program.

You can use the tar command to list the content of the file that the uncompress command is reading.

# uncompress -c test.tar.Z | tar tvf -
-rw-r--r-- root/root        31 2017-12-02 07:02 test
-rw-r--r-- root/root        19 2017-12-02 07:04 test1
-rw-r--r-- root/root        20 2017-12-02 07:04 test2

The dash (-) at the end of the command line indicates that the tar command reads the data from the piped output of the uncompress command rather than a tar file or a disk.

Compressing a File: gzip Command

Alternatively, you can also use the gzip command to compress files.

$ gzip [ -v ] filenames

The gzip command performs the same function as the compress command, but the gzip command generally produces smaller files. For example, to compress a set of files, file1, file2, file3, and file4, enter the following command:

 $ gzip file1 file2 file3 file4
$ ls *.gz
file1.gz file2.gz file3.gz file4.gz

The compressed files have a .gz extension.

Viewing a Compressed File: gzcat Command

The gzcat command displays files that were compressed with either the gzip or compress commands.

$ gzcat filename

To view the data.txt.gz file, use the following command:

# gzcat data.txt.gz 
northwest       NW      Joel Craig          10
western         WE      Sharon Kelly        40
southwest       SW      Chris Foster        33
northeast       NE      TJ Nichols          67
north           NO      Val Shultz          91
central         CT      Sheri Watson        44
Note: The gzcat command does not change the content of the compressed file. The compressed file remains on the disk in the compressed form.

Uncompressing a File: gunzip Command

The gunzip command uncompresses a file that has been compressed with the gzip command.

$ gunzip filename

To uncompress the file1.gz file, use the following command:

$ gunzip file1.gz

Compressing and Archiving Multiple Files: zip Command

The zip command compresses and archives multiple files into a single file in one go.

$ zip target_filename source_filenames

To compress test1 and test2 into the test.zip archive file, enter the following command:

# zip test.zip test1 test2
  adding: test1 (deflated 5%)
  adding: test2 (stored 0%)
# ls -l test.zip 
-rw-r--r--. 1 root root 336 Dec  8 05:32 test.zip

By default, the zip command adds the .zip extension to the compressed archive file if you do not assign a new file name with an extension.

Note: You can run the zip or unzip command on the command line to view a list of options used with each command.

Viewing and Uncompressing Archive Files: unzip Command

The unzip command is used for listing the files and also for extracting the content of a compressed .zip file.

$ unzip zipfile

To uncompress the file.zip archive file, use the following command:

$ unzip file.zip
Related Post