May 4, 2024

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
Viewing contents of gzip file in Linux

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.

Leave a Reply

%d bloggers like this: