How To Read Gz File In Linux : Decompressing And Viewing Contents

If you’ve ever wondered how to read gz file in linux, you’re not alone. Many users need to inspect compressed log files, backups, or data archives without wasting time on full extraction. The good news is that Linux offers several built-in commands that let you view these files directly, keeping them compressed and saving disk space.

This guide covers all the practical methods for reading `.gz` files in Linux, from quick terminal commands to advanced scripting. You’ll learn how to peek inside compressed files without decompressing them first, making your workflow faster and more efficient.

Understanding Gzip Compression In Linux

Gzip (GNU zip) is one of the most common compression tools on Linux systems. It uses the DEFLATE algorithm to reduce file sizes, which is especially useful for logs, text files, and backups. A `.gz` file is simply a compressed version of the original file, often with the original filename plus `.gz` extension.

When you compress a file with `gzip`, it replaces the original with a compressed version. For example, `gzip data.txt` creates `data.txt.gz` and removes the original. This is different from `tar.gz` or `.tgz` files, which are archives compressed with gzip.

Key Differences Between Gzip And Other Compression Tools

  • gzip – Compresses single files, not directories
  • tar.gz – Combines multiple files into an archive, then compresses
  • bzip2 – Slower but better compression ratio
  • xz – Even higher compression, slower

How To Read Gz File In Linux

Now let’s get to the core of this article. The most efficient way to read a `.gz` file is using commands that decompress on the fly. These tools pipe the decompressed content directly to your terminal or to other commands, so you never need to create a temporary uncompressed file.

Method 1: Using Zcat (The Simplest Option)

`zcat` is essentially `gzip -dc`, which decompresses and outputs to stdout. It works exactly like `cat` but for compressed files.

  1. Open your terminal
  2. Type: zcat filename.gz
  3. Press Enter to see the content

Example: zcat access.log.gz will show the entire log file. If the file is large, the output will scroll quickly. Use zcat filename.gz | less to paginate.

Method 2: Using Zless And Zmore For Pagination

When files are too long for one screen, `zless` and `zmore` are your friends. They work like `less` and `more` but handle `.gz` files automatically.

  • zless filename.gz – Opens the file in a scrollable view (use arrow keys, Page Up/Down, press `q` to quit)
  • zmore filename.gz – Similar but with more basic navigation

These commands are ideal for reading log files or configuration files without decompressing them.

Method 3: Using Gzip -D With Pipes

You can also use `gzip -d` (decompress) with a pipe to other commands. This gives you more control over what you see.

gzip -dc filename.gz | head -n 20
gzip -dc filename.gz | tail -n 50
gzip -dc filename.gz | grep "error"

The `-c` flag keeps the original file compressed while sending decompressed data to stdout. This is perfect for searching or extracting specific lines.

Method 4: Using Zgrep For Pattern Searching

If you need to find specific text inside a compressed file, `zgrep` is the tool. It works like `grep` but handles `.gz` files directly.

Example: zgrep "404" access.log.gz will show all lines containing “404” from the compressed log. You can use all standard grep options like `-i` for case-insensitive or `-v` for inverse matching.

Method 5: Using Zcat With Other Commands

Combine `zcat` with other Linux utilities for powerful one-liners:

  • zcat file.gz | wc -l – Count lines
  • zcat file.gz | sort – Sort the content
  • zcat file.gz | awk '{print $1}' – Extract first column

This approach keeps your workflow efficient without creating temporary files.

Reading Compressed Files Without Decompressing

The beauty of these tools is that they never modify the original `.gz` file. You can read, search, and process compressed data while it stays compressed on disk. This is critical when working with large files where decompression would take time and fill up disk space.

Viewing Specific Parts Of A Gz File

Sometimes you only need a portion of the file. Here are practical examples:

# First 10 lines
zcat file.gz | head -n 10

# Lines 100-120
zcat file.gz | sed -n '100,120p'

# Last 30 lines
zcat file.gz | tail -n 30

Checking File Metadata Without Reading Content

Before reading a `.gz` file, you might want to check its size and compression ratio:

gzip -l filename.gz

This shows the compressed size, uncompressed size, and ratio. It’s useful for estimating how much data you’re about to view.

Working With Tar.Gz Files

Many `.gz` files are actually tar archives (`.tar.gz` or `.tgz`). To read these, you need to list or extract the archive contents first.

Listing Contents Without Extraction

tar -tzf archive.tar.gz

The `-t` flag lists contents, `-z` handles gzip compression, and `-f` specifies the file. You’ll see all files inside the archive.

Extracting Specific Files

tar -xzf archive.tar.gz file1.txt file2.txt

This extracts only `file1.txt` and `file2.txt` from the archive. Use `-C` to specify a target directory.

Viewing A Single File Inside Tar.Gz

To read one file without extracting the whole archive:

tar -xzf archive.tar.gz --to-stdout filename.txt | less

This pipes the decompressed file directly to `less` for viewing.

Advanced Techniques For Reading Gz Files

Once you’re comfortable with basic commands, you can build more complex workflows.

Reading Multiple Gz Files At Once

zcat *.gz | less

This concatenates all `.gz` files in the current directory and shows them in `less`. Be careful with large sets of files.

Using Zcat With Find Command

find /var/log -name "*.gz" -exec zcat {} \; | grep "ERROR"

This searches for “ERROR” across all compressed log files in `/var/log`. The `-exec` option runs `zcat` on each found file.

Reading Gz Files Remotely

If the file is on a remote server, you can combine SSH with these commands:

ssh user@server "zcat /path/to/file.gz" | less

This reads the compressed file remotely without transferring the entire decompressed content.

Common Errors And Troubleshooting

Even experienced users run into issues. Here are solutions to frequent problems.

“Command Not Found” Errors

If `zcat` or `zless` are missing, install the gzip package:

# Debian/Ubuntu
sudo apt install gzip

# RHEL/CentOS
sudo yum install gzip

Binary Content In Output

Some `.gz` files contain binary data that looks garbled when viewed. Use `strings` to extract readable text:

zcat file.gz | strings

Corrupted Gz Files

If you get “unexpected end of file” errors, the file may be incomplete. Try:

gzip -t filename.gz

This tests the integrity of the compressed file. If it fails, you may need to download or recreate the file.

Performance Considerations

Reading large `.gz` files can be resource-intensive. Here are tips for better performance.

  • Use `zcat` with `head` or `tail` to avoid reading the entire file
  • For huge files, consider using `zstd` (Zstandard) which is faster than gzip
  • Use `nice` to lower priority if the system is under load: `nice -n 19 zcat bigfile.gz`

Comparing Speed Of Different Methods

In practice, `zcat` and `gzip -dc` have similar performance. `zless` adds overhead for pagination. For pure speed, pipe `zcat` directly to what you need.

Scripting With Gz Files

Automate repetitive tasks with simple scripts. Here’s a bash function to read a compressed file with line numbers:

function gzcatn() {
    zcat "$1" | cat -n | less
}

Add this to your `.bashrc` and use `gzcatn file.gz` to see numbered lines.

Processing Gz Files In A Loop

for file in *.gz; do
    echo "Processing $file"
    zcat "$file" | head -n 5
done

This processes all `.gz` files in the current directory, showing the first 5 lines of each.

Alternatives To Gzip Commands

While the standard tools work well, there are alternatives worth knowing.

Using Vim To Read Gz Files

Vim can open `.gz` files directly:

vim filename.gz

Vim decompresses the file in memory and recompresses on save. This is useful for editing compressed files.

Using Emacs

Similarly, Emacs handles `.gz` files transparently. Open the file normally and it will be decompressed for editing.

Using Midnight Commander

Midnight Commander (mc) can browse inside `.gz` files. Navigate to the file and press Enter to view its contents.

Security Considerations

When reading `.gz` files from untrusted sources, be cautious. Compressed files can contain malicious content. Always verify the source and consider using `file` command to check the type:

file filename.gz

This confirms it’s a genuine gzip compressed file and not something else.

Avoiding Command Injection

If you’re processing filenames from user input, sanitize them to prevent injection attacks. Use `–` to separate options from filenames:

zcat -- "$filename"

Frequently Asked Questions

Can I Read A .Gz File Without Decompressing It?

Yes, commands like `zcat`, `zless`, and `zgrep` read compressed files directly without creating decompressed copies. They decompress on the fly and show the content in your terminal.

What Is The Difference Between Zcat And Gunzip -C?

There is no practical difference. `zcat` is a symlink to `gunzip -c` on most systems. Both decompress to stdout while keeping the original file intact.

How Do I Read A .Tar.gz File Without Extracting It?

Use `tar -tzf file.tar.gz` to list contents. To view a specific file inside, use `tar -xzf file.tar.gz –to-stdout filename.txt | less`.

Why Does Zcat Show Binary Garbage For Some Files?

Some `.gz` files contain binary data (like images or databases) that isn’t human-readable. Use `strings` to extract text portions, or check the file type with `file` command.

Can I Read A .Gz File From A URL Directly?

Yes, combine `curl` with `zcat`: `curl -s http://example.com/file.gz | zcat | less`. This streams the compressed file and decompresses it on the fly.

Conclusion

Reading `.gz` files in Linux is straightforward once you know the right commands. Whether you’re checking logs, inspecting backups, or processing data, tools like `zcat`, `zless`, and `zgrep` let you work efficiently without wasting disk space or time on decompression. Start with the basics, then experiment with pipes and scripts to build your own workflows. With practice, you’ll handle compressed files as easily as regular ones.