LEARN HOW TO READ LINUX GZIP COMPRESSED LOG FILES

Today in this post, we will guide you through how can you read and work on gzipped files.

This tutorial will be most helpful for those who would like to be more efficient in using the Linux command line. Don’t be worried if you are a Linux beginner because you will be able to follow through and increase your pace with time.

reading log files

READING GZIP COMPRESSED FILES IN LINUX COMMAND LINE

You might have noticed while working on backend logs that they are mostly compressed with the .gz extension. This is quite common because compressed log files save a lot of storage and implicitily, the cost of storage.

However, the log files are not like the regular text files whose comtent can be easily seen, or on which grep can be run on, or less to read the content without flooding your screen. Files that have been compressed cannot be used with the same regular Linux commands.

The good news is that whenever you have gzipped files you also have the powerful Z commands to work on them with.

These Z commands provide a ‘Z’ equivalent of the regular file manipulation commands and the best thing about using them is that you don’t have to extract the compressed files. You can use the commands directly on them.

Providing you with;

  • zcat for cat to view compressed file
  • zdiff for diff to see the difference between two compressed files
  • zgrep for grep to search inside the compressed file
  • zless for less, zmore for more, to view the file in pages

VIEWING COMPRESSED FILES WITH ZCAT

Replace cat with zcat as zcat is used in exactly the same manner as cat. Take for example:

zcat logfile.gz

This will cause all the contents of logfile.gz to be displayed without even extracting them. (Althoug, techinically, they are temporarily extracted in /tmp)

Use regular less and more commands with zcat to see the output in pages like so:

zcat logfile.gz | less
zcat logfile.gz | more

If you don’t know whether or not the file is compressed you can simply use zcat with option -f to display the content of the file irrespective of whether it is gzipped or not.

zcat -f logfile.gz

READING COMPRESSED FILES WITH ZLESS AND ZMORE

Just like less and more, you can use zless and zmore to read the content of the compressed files without decompressing the files. All the keyboard shortcuts of less and more work the same way.

zless logfile.gz
zmore logfile.gz

SEARCHING INSIDE COMPRESSED FILES WITH ZGREP

Grep is powerful and it is one of the most used Linux commands today. zgrep is the Z counterpart of grep with which you can search inside gzipped compressed files without extracting it.

It can be used with all the regular gerp options. Take for example:

zgrep -i keyword_search logfile.gz

COMPARING COMPRESSED FILES WITH ZDIFF

This might not be all that useful on huge log files, zdiff can be used to see the difference between compressed files in the same way diff is used.

zdiff logfile1.gz logfile2.gz

Those are some of the commands frequently used to work on compressed log files in the Linux command line. Have you got more command tricks for compressed files up your sleeves? Feel free to share them with us.

About The Author
Okoi Martins Jr.
I'm a Computer Scientist with a passion for learning new things in fields ranging from theoretical implications of computer science and mathematical modeling to web development and music. In my spare time, I listen to music, read like a compiler, and learn like an A.I algorithm.

Leave a Response