View compressed log files with Zcat
By: Roger Creasy
Have you ever need to view log files after they have been rotated and compressed? Many people uncompress the file with gunzip or gzip -d then view the file. Linux gives us a shortcut, namely zcat. Zcat opens a compressed file immediately for viewing. However, when used alone against a compressed log file you will likely get too much text on the screen. The easy solution is to pipe the zcat results to less.
zcat large-compressed-log.gc | less
By doing this you can navigate through the file with the enter key, the space bar, and the up and down arrow keys. You also gain the ability to search using the / key. Exit less with the q key.
You gain some other functionality by piping to less.
If you want to view the file beginning at a known line number, add "+#" after less.
zcat large-compressed-log.gc | less +2500
To have the lines numbered, add "-N"
zcat large-compressed-log.gc | less -N
To open the file at the first occurrence of a search term, and highlight all occurrences of the search term, add "+/search term"
zcat large-compressed-log.gc | less +/fail2ban
Zcat combined with less is a powerful way to dive into your compressed log files, or any large compressed file for that matter.
I hope you have found this post beneficial. If you have, and like it, please share. If you know of any mistakes or omissions, please let me know on Twitter @RogerCreasy
Thanks for reading!