On Linux servers, logs are often compressed in gzip format to save disk space.
If you are investigating some issue and you have to deal with gzip compressed logs, the normal workflow is to extract the .gz log files first and then use cat, less, grep etc commands to read and analyze the logs.
Why? Unlike regular text files, where you can use the cat command to viewcontent of the file or use grep command on it or use less to read the content without flooding your screen, compressed files cannot be used with the same regular Linux commands.
But extracting the compressed log files first and then analyzing them takes more time and disk space. You extract all the required files one by one, analyze them and then remove the extracted files when you don't need them anymore.
There is a better way. Use Z commands!
Dealing with Gzip compressed files without extracting them
Not many people are aware of the powerful 'Z commands' that work on the gzipped files without you needing to extract them first. They work directly on compressed files.
Actually, it does kind of extract it temporarily in /tmp, but thatβs not the same as an actual extraction, is it?
These Z commands provide a βZβ equivalent to regular file manipulation commands.
So, you get:
- zcat: cat to view compressed file
- zgrep: grep to search inside the compressed file
- zless for less, zmore for more: to view the file in pages
- zdiff: diff to see the difference between two compressed files
Don't worry too much. You don't have to learn new command syntax. These Z commands work pretty much the same as their regular counterpart for the most popular options.
Let's see it in action.
Viewing compressed files with zcat
If you use the cat command, you can replace it with zcat
. zcat is used in exactly the same manner as you use cat. For example:
zcat logfile.gz
This will display all the contents of logfile.gz without even extracting it.
You can use regular less and more commands with zcat
to see the output in pages:
zcat logfile.gz | less
zcat logfile.gz | more
If you donβt know if the file is compressed (i.e., files without .gz extension), you can use zcat with option -f. This will 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
Same as 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.
zless logfile.gz
zmore logfile.gz
Searching inside compressed files with zgrep
Grep is a hell of a powerful command and I think, one of the most used Linux commands. zgrep
is the Z counterpart of grep that allows you to search inside gzipped compressed files without extracting them.
You can use it with all the regular grep options. For example:
zgrep -i keyword_search logfile.gz
Comparing compressed files with zdiff
While this might not be that useful on huge log files, you can use zdiff to see the difference between compressed files, in the same way as you use the diff command.
zdiff logfile1.gz logfile2.gz
Speaking of diff, you may want to look at Meld GUI diff tool.
Summary
Command | Use | Example |
---|---|---|
zcat | cat to view compressed file |
zcat <path_to_gzipped_log_file> |
zgrep | grep to search inside the compressed file |
zgrep -i <path_to-gzipped_log_file> |
zless | less to view the compressed file in pages |
zless <path_to_gzipped_log_file> |
zmore | more to view the compressed file in pages |
zmore <path_to_gzipped_log_file> |
zdiff | diff to see the difference between two compressed files |
zdiff <path_to_first> <path_to_second> |
Now you know how to work with gzipped files. Check out more about the gzip command in Linux:
Or perhaps you would want to learn about analyzing journal logs.
The Z commands are awesome! And I know that many people get their 'Eureka moment' when they first learn about it.
What about you? Did you find these z commands useful? The comment section is all yours.