How to Read Contents of Compressed File in Linux

On Linux servers, logs are often compressed in gzip format to save disk space because ‘/var/log’ filesystem mounted separately with small size, it would be 5-8GB.

You can examine the contents of compressed files on Linux systems without uncompressing/extracting the files using 'z*' commands. These commands allow you to read gzip compressed text files using zless, zcat, bzcat, and zmore commands.

This post explains how to read the contents of compressed files and directories in Linux without extracting them, by leaving the file intact.

Why do we need to read compressed files without extracting them?

Normally, the user would decompress the file without the '-C' parameter, which would replace the contents to the current log file, if it was compressed with the full path. Moreover you will lose current logs and because of this syslog will stop writing a live log to the file unless restarted. Therefore, to avoid this situation, we recommend reading uncompressed log files, but this is not only the reason for this, but also other facts should be considered.

1) Viewing the contents of compressed files in Linux

You can use any of the below Z commands based on your requirement to achieve this task.

CommandUsageExample
zcat/bzcatcat to view the compressed files with .gz & .bz2zcat [Path/to/test.gz] / bzcat [Path/to/test.bz2]
zless/bzlessless to view the compressed files in pages with .gz & .bz2zless [Path/to/test.gz] / bzless [Path/to/test.bz2]
zmore/bzmoremore to view the compressed files in pages with .gz & .bz2zmore [Path/to/test.gz] / bzmore [Path/to/test.bz2]
zgrepgrep to search matching string inside the compressed filezgrep -i [String] [Path/to/test.gz]
zdiffdiff to see the difference between two compressed fileszdiff [Path/to/test1.gz] [Path/to/test2.gz]

1.1) Viewing contents of ZIP file

To view the contents of a ZIP file without extracting, simply use zcat, zmore or zless command followed by the filename.

zcat FossTechi_Test.zip
or
zmore FossTechi_Test.zip
or
zless FossTechi_Test.zip

1.2) Reading contents of GZIP file

Same as less and more, you can use any of the below commands to view the contents of a gzip file without decompressing it in Linux.

zcat FossTechi.gz
or
zmore FossTechi.gz
or
zless FossTechi.gz

1.3) Viewing contents of bzip2/bz2 file

You can use any of the below commands to view the contents of a bzip2/bz2 file without extracting it in Linux.

bzcat FossTechi_Test.bz2
or
bzmore FossTechi_Test.bz2
or
bzless FossTechi_Test.bz2

1.4) Searching inside compressed files with zgrep

grep is a very powerful command in Linux and zgrep allows you to search given keywords inside gzipped compressed files without extracting them.

zgrep -i keyword_search FossTechi_Test.gz

1.5) Comparing compressed files with zdiff

Similarly, you can use zdiff command to see the difference between gzip compressed files.

zdiff FossTechi_1.gz FossTechi_2.gz

Conclusion

In this short article, we have learned how to read compressed files without extracting them in Linux using zcat, zless, zmore, bzcat, bzless, bzmore, zgrep, and zdiff commands.

fosstechi

Recent Posts

How to List or Display all Cron Jobs in Linux

Cron is a command-line job scheduler available on Linux operating systems that allows users to…

1 year ago

How to Disable IPv6 on RHEL System

In computing, there are two types of IP addresses, IPv4 and IPv6. By default, IPv6…

1 year ago

How to View Contents of tar.gz File in Linux

A tarball is commonly known as a TAR file, short for tape archive, which is…

1 year ago

How to Create tar.gz File in Linux

A tar archive is a file that stores a collection of directories and files, also…

1 year ago

How to Identify LUN Mapped to VxVM Disk in Linux

On Linux, you can quickly find out how LUNs are mapped to the underlying OS…

1 year ago

How to List Available Package Updates in Ubuntu

Keeping your Ubuntu system up-to-date is critical to ensuring a system's stability and security. This…

1 year ago

This website uses cookies.