Managing Disk Space   «Prev  Next»
Lesson 4 Viewing compressed files
ObjectiveUse zcat to view compressed files.

Viewing compressed files using zcat in Unix

Sometimes you’d like to see what’s inside a compressed file. To do so, you can uncompress the file, look inside (using cat or more, for example), and then compress the file again. But this is inconvenient, especially if there are several compressed files you’d like to check. An easier way is to use the zcat command, like this:
% zcat file

zcat reads the given file without restoring the original version. In other words, zcat decodes the file and displays it on your screen. Typically, you pipe the output of zcat to another program, such as more or grep. For example, to search for the name Daniel in a compressed file named big_list.Z, you would enter:
% zcat big_list | grep Daniel
Note that I omitted the .Z extension in the above command. Like the uncompress command, zcat doesn’t care whether you specify the .Z or not. View the following series of images below to explore how you can use zcat .

The pwd command shows your current location, and `ls` lists two files. Note that `test_plan.html` is compressed, but `index.html` is not.
1) "The `pwd`command shows your current location, and `ls`lists two files. Note that `test_plan.html`is compressed, but `index.html` is not."
Code:
danielg % pwd
/export/home/danielg/project/qa
danielg % ls
index.html       test_plan.html.Z
danielg %

Interpretation:
Key Takeaway: The image illustrates basic Unix/Linux commands to show the current directory and list the files within it. The important point highlighted is that one of the files (`test_plan.html.Z`) is compressed, while the other (`index.html`) is not.

The zcat command displays the compressed file, test_plan.html, and then pipes the output to more.
2) "The zcat command displays the compressed file, test_plan.html, and then pipes the output to more. The .Z extension was not needed when specifying the file name."
Code:
danielg % zcat test_plan.html | more
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <meta name="author" content="Peter Mock">
    <meta name="GENERATOR" content="Mozilla/4.51 [en] (WinNT: I) [eCyberCom]">
    <title>eCyberCom 1.0 Test Plan</title>
</head>
<body>

--More--


Interpretation:
  • `zcat test_plan.html | more`: The `zcat` command is used to view the contents of a compressed file without decompressing it first. In this example, it reads the compressed `test_plan.html.Z` file and pipes the output to the `more` command, which allows you to view the file contents one screen at a time.
  • Note on File Extension: The `.Z` extension is not required when specifying the file name in the `zcat` command, meaning you can simply refer to the file as `test_plan.html` rather than `test_plan.html.Z`.

Key Takeaway: The image demonstrates how to view the contents of a compressed file using `zcat`, even when the file has a `.Z` extension. The output is piped to `more` to make it easier to read through the contents. This technique is particularly useful when working with large compressed files in Unix/Linux environments.


If you try zcat with an uncompressed file such as index.html, the command fails.
3) If you try zcat with an uncompressed file such as index.html, the command fails. The 'no such file' message is generated because zcat expects its arguments to be compressed files.
Code:
danielg % zcat index.html
index.html.Z: No such file or directory
danielg %
Interpretation:
  • zcat index.html: The zcat command is designed to read and display the contents of compressed files (typically with a .Z or .gz extension). In this case, attempting to use zcat on an uncompressed file (index.html) results in an error.
  • Error Message: The error message "No such file or directory" occurs because zcat is looking for a compressed version of index.html, typically named index.html.Z, which does not exist.



Some UNIX systems provide the zmore command, which is similar to more but works with compressed files. Instead of entering zcat file | more, you can simply enter zmore file.

Function of the zmore command in Unix

The zmore command in Unix is a command-line utility that allows you to view the contents of compressed text files on the terminal. It is a simple way to view compressed files without having to first decompress them to an uncompressed file. The zmore command works by using the more command to page through the contents of the compressed file, while also using the zcat command to decompress the file on-the-fly. This allows you to view the contents of the compressed file as if it were an uncompressed text file.
Here is the basic syntax for the zmore command:
zmore [OPTIONS] [COMPRESSED_FILE]

Here are some common options for the zmore command:
  1. -h or --help: display help information
  2. -q or --quiet: suppress informational messages
  3. -p or --prompt: change the prompt for the more command
  4. -c or --clear-screen: clear the screen before displaying the file
Here are some examples of how to use the zmore command:
  1. To view the contents of a compressed file:
    zmore file.gz
    

  2. To view the contents of a compressed file without informational messages:
    zmore -q file.gz
    

  3. To view the contents of a compressed file with a custom prompt:
    zmore -p "Enter to continue..." file.gz
    

  4. To clear the screen before displaying the file:
    zmore -c file.gz
    

  5. The zmore command is often used in combination with other Unix commands to process or manipulate compressed text files. For example, piping the output of zmore to the grep command can be used to search for specific patterns within compressed files, while piping the output of zmore to the sed command can be used to perform text substitutions within compressed files.

Disk Usage file Compression - Quiz

Click the Quiz link below to test your knowledge of disk usage and file compression.
Disk Usage file Compression - Quiz
In the next lesson, you will learn about file archives.

SEMrush Software 4 SEMrush Banner 4