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 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:
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.
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.
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.