How to Compress, Uncompress and view Compressed files in Linux

The ‘compress’ Command

Compressing files helps you to conserve disk space. This section describes how to use the compress command to reduce the size of a file. You can use the compress command to reduce the size of a file. You can compress large files to reduce the use of disk space as well as the time required for file transfers over a network. The amount of compression depends on the type of file you compress. Typically, compression reduces a text file by 50 percent to 60 percent.

The syntax for the compress command is:

$ compress [ -v ]filename

For example:

$ compress dante

When you compress a file, the compress command replaces the original file with a new file that has a .Z extension. The ownership and modification time of the original file remain the same, but the contents of the file change.

Compressing a File

The following example shows you how to compress a file called files.tar, using the -v (verbose) option. Using this option with the compress command displays a message providing information about the percentage reduction or expansion of each file.

$ compress -v files.tar
files.tar: Compression: 70.20% -- replaced with files.tar.Z

The compressed file, which replaces the files.tar file, is called files.tar.Z. When a file has a .Z extension it is a compressed file, and you should not view or print such a file without first uncompressing it.

If you do not use the-v(verbose) option with the compress command, no output appears when you perform the command. For example:

$ compress files.tar
$

Viewing Compressed Files by Using the zcat Command

You can print the uncompressed form of a compressed file to standard output. This section describes how to view the uncompressed form of a compressed file by using the zcat command. You can use the zcat command to view files that were compressed by using the compress command. The zcat command interprets the compressed data and displays the contents of a file as if it were not compressed. The zcat command does not change the contents of a compressed file.

The syntax for the zcat command is:

zcat filename

Viewing the Contents of a Compressed File

The following example shows you how to view the contents of the compressed file called dante.Z.

$ zcat dante.Z | more
This is a sample file
to test the functionality of zcat
on linux operating systems

You can use the pipe (|) character with the zcat command to extract the contents of a compressed tar file without uncompressing the tar file first. For example:

$ zcat files.tar.Z | tar xvf -

The dash (-) at the end of the tar command indicates that the tar command should read the tar file from standard input instead of reading the tar file from a file or tape. In this example, the standard input comes from the output of the zcat command because of the pipe (|).

Uncompressing Files by Using the uncompress Command

You can restore a compressed file to its original state after it has been compressed. This section describes how you can uncompress files by using the uncompress command. The uncompress command restores a compressed file back to its original state. The syntax for the uncompress command is:

uncompress options filename

Uncompressing a File

The following example shows you how to uncompress the files.tar.Z file and replace it with the original file named files.tar. The -v option causes the uncompress command to display messages about the action being performed.

$ uncompress -v files.tar.Z
files.tar.Z:  -- replaced with files.tar
$

Viewing the Contents of a Compressed File

You can use the uncompress command with the -c option to send the contents of a compressed file to the screen (stdout), without changing the compressed (.Z) file. 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 contents of the file that the uncompress command is reading.

$ uncompress -c files.tar.Z | tar tvf-
tar: blocksize = 11
-rw-rw---- 1233/10    1610 May 7 14:12 2020 file1
-rw-rw---- 1233/10     105 May 7 14:12 2020 file2
-rw-rw---- 1233/10     218 May 7 14:12 2020 file3

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 tape.

Compressing a File by Using the gzip Command

You can also use the gzip command to reduce the size of files. This section describes how to compress a file by using the gzip command. The gzip command reduces the size of files. When the gzip command successfully compresses a file, the original file is replaced by a file with the same name and a.gz extension. The files keep the same ownership modes, access, and modification times. The gzip command is used to compress regular files. The syntax for the gzip command is:

gzip [ -v ] filenames

The following example shows you how to compress the file1, file2, file3, and file4 files with the gzip command.

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

Restoring a gzip File by Using the gunzip Command

You can use the gunzip command to restore a file that has been compressed with the gzip command. The following example shows you how to uncompress the file1.gz file.

$ gunzip file1.gz

Viewing Files by Using the gzcat Command

You can use the gzcat command to look at a compressed file. This section describes how to view files by using the gzcat command. You can use the gzcat command to view files that were compressed with either the gzip command or the compress command.

The gzcat command interprets the compressed data and displays the contents of the file as if it were not compressed. The gzcat command does not change the contents of the compressed file. The compressed file remains on the disk in compressed form. The syntax for the gzcat command is:

gzcat filename

Viewing the Contents of a Compressed File

You can use the gzcat command to view the contents of a compressed tar file without having to first uncompress the file. The following example shows you how to view the file1.gz file.

$ gzip file1
$ ls file1*
file1.gz
$ gzcat file1.gz
The compress utility reduces the size of files using adaptive Lempel-Ziv coding.  Each file is renamed to the same name plus the extension .Z.
A file argument with a .Z exten-sion will be ignored except it will cause an error exit after other arguments are processed.
If compression would not reduce the size of a file, the file is ignored.

Compressing and Archiving Multiple Files by Using the zip Command

You can compress and archive files with a single command. The following section describes how to perform this combined task by using the zip command. The zip command compresses multiple files into a single archive file. The zip command adds the .zip extension to the file name of the compressed archive file if you do not assign a new file name with an extension.

The syntax for the zip command is:

zip target_filename source_filenames

The following example shows you how to use the zip command to compress the file2 and file3 files into the file.zip archive file.

$ zip file.zip file2 file3
adding: file2 (deflated 16%
adding: file3 (deflated 26%)
$ ls
file.zip
file2
file3

To list the files in a zip archive, perform the unzip -l command.

$ unzip -l zipfile

Restoring a zip File by Using the unzip Command

You can uncompress the contents of a zip file by using the unzip command. The following example shows you how to uncompress the file.zip archive file.

$ unzip file.zip