{"id":3427,"date":"2014-08-06T01:37:10","date_gmt":"2014-08-05T17:37:10","guid":{"rendered":"http:\/\/rmohan.com\/?p=3427"},"modified":"2014-08-06T02:49:00","modified_gmt":"2014-08-05T18:49:00","slug":"zcat-shell-bash","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3427","title":{"rendered":"ZCAT Shell bash"},"content":{"rendered":"<p>How to display the contents of a gzip\/gz file<br \/>\nBy Alvin Alexander. Last updated: Aug 6, 2011<br \/>\nProblem: You have a plain text file that has been compressed with the gzip command, and you&#8217;d like to display the file contents with the Unix\/Linux cat or more commands.<\/p>\n<p>Solution: Instead of using the cat or more commands, use their equivalents for working with gz files, the zcat and zmore commands.<\/p>\n<p>For instance, if you want to display the contents of an Apache log file (which is a plain text file) that has been compressed with gzip, just use the zcat command, like this:<\/p>\n<p>zcat access_log.gz<br \/>\nOf course almost any Apache log file will be large, and will scroll off the screen quickly, so you&#8217;ll probably want to use the gzip equivalent of the more command, zmore, like this:<\/p>\n<p>zmore access_log.gz<\/p>\n<p>find . -name &#8220;*.gz&#8221; | while read -r file; do zcat -f &#8220;$file&#8221; | head -n 1; done<\/p>\n<p>zcat `man -w manpage` | groff -mandoc -T html &#8211; > filename.html<\/p>\n<p>save manpage as html file<\/p>\n<p>zcat log.tar.gz | grep -a -i &#8220;string&#8221;<\/p>\n<p>grep compressed log files without extracting. Useful in system where log files are compressed for archival purposes<\/p>\n<p>zcat \/usr\/share\/man\/man1\/man.1.gz | nroff -man | less<\/p>\n<p>As odd as this may be, I know of servers where the man(1) command is not installed, and there is not enough room on \/ to install it. However, zcat(1), nroff(1) and less(1) are. This is a way to read those documents without the proper tool to do so, as sad as this may seem. \ud83d\ude42<\/p>\n<p>This command enables the user to append a search pattern on the command line when using less as the PAGER. This is especially convenient (as the example shows) in compressed files and when searching man pages (substituting the zcat command with man, however).<\/p>\n<p><strong>zcat -f $(ls -tr access.log*)<br \/>\n<\/strong><br \/>\nconcatenate compressed and uncompressed logs<br \/>\nwith zcat force option it&#8217;s even simpler.<\/p>\n<p>find \/var\/log\/apache2 -name &#8216;access.log*gz&#8217; -exec zcat {} \\; -or -name &#8216;access.log*&#8217; -exec cat {} \\;<br \/>\nfunctions: cat find zcat<br \/>\nconcatenate compressed and uncompressed logs<br \/>\nThis command allows you to stream your log files, including gziped files, into one stream which can be piped to awk or some other command for analysis.<br \/>\nNote: if your version of &#8216;find&#8217; supports it, use:<\/p>\n<p>find \/var\/log\/apache2 -name &#8216;access.log*gz&#8217; -exec zcat {} + -or -name &#8216;access.log*&#8217; -exec cat {} +<br \/>\nzcat database.sql.gz | mysql -uroot -p&#8217;passwd&#8217; database<br \/>\nFunctions: zcat<br \/>\nRestore mysql database uncompressing on the fly.<br \/>\nThis way you keep the file compressed saving disk space.<br \/>\nOther way less optimal using named pipes:<br \/>\nmysql -uroot -p&#8217;passwd&#8217; database <\n\n\n( zcat $FILE || gzcat $FILE || bzcat2 $FILE ) | less\nGroup OR'd commands where you expect only one to work\nSomething to stuff in an alias when you are working in multiple environments. The double-pipe OR will fall through until one of the commands succeeds, \nand the rest won't be executed. Any STDERR will fall out, but the STDOUT from the correct command will bubble out of the parenthesis to the less command, or some other command you specify.\n\n\n( last ; ls -t \/var\/log\/wtmp-2* | while read line ; do ( rm \/tmp\/wtmp-junk ; zcat $line 2>\/dev\/null || bzcat $line ) > \/tmp\/junk-wtmp ; last -f \/tmp\/junk-wtmp ; done ) | less<br \/>\nFunctions: last ls read rm zcat<br \/>\nTags: last command wtmp<br \/>\nSee a full last history by expanding logrotated wtmp files<br \/>\nWhen your wtmp files are being logrotated, here&#8217;s an easy way to unpack them all on the fly to see more than a week in the past. The rm is the primitive way to prevent symlink prediction attack.<\/p>\n<p>zcat access_log.*.gz | awk &#8216;{print $7}&#8217; | sort | uniq -c | sort -n | tail -n 20<br \/>\nFunctions: awk sort tail uniq zcat<br \/>\nTags: log apache zcat analysis<br \/>\nAnalyse compressed Apache access logs for the most commonly requested pages<\/p>\n<p>sudo zcat \/var\/log\/auth.log.*.gz | awk &#8216;\/Failed password\/&#038;&#038;!\/for invalid user\/{a[$9]++}\/Failed password for invalid user\/{a[&#8220;*&#8221; $11]++}END{for (i in a) printf &#8220;%6s\\t%s\\n&#8221;, a[i], i|&#8221;sort -n&#8221;}&#8217;<br \/>\nFunctions: awk printf sudo zcat<br \/>\nTags: Security awk brute force<br \/>\nShow the number of failed tries of login per account. If the user does not exist it is marked with *.<\/p>\n<p>zcat a_big_file.gz | sed -ne &#8220;$(zcat a_big_file.gz | tr -d &#8220;[:print:]&#8221; | cat -n | grep -vP &#8220;^ *\\d+\\t$&#8221; | cut -f 1 | sed -e &#8220;s\/\\([0-9]\\+\\)\/\\1=;\\1p;\/&#8221; | xargs)&#8221; | tr -c &#8220;[:print:]\\n&#8221; &#8220;?&#8221;<\/p>\n<p>Functions: sed tr zcat<br \/>\nScan a gz file for non-printable characters and display each line number and line that contains them.<br \/>\nScans the file once to build a list of line numbers that contain non-printable characters<br \/>\nScans the file again, passing those line numbers to sed as two commands to print the line number and the line itself. Also passes the output through a tr to replace the characters with a ?<\/p>\n<p>zcat \/usr\/share\/doc\/vim-common\/README.gz | vim -g +23 &#8211;<br \/>\nFunctions: vim zcat<br \/>\nPipe a textfile to vim and move the cursor to a certain line<br \/>\nThis command is more for demonstrating piping to vim and jumping to a specific line than anything else.<br \/>\nExit vim with :q!<br \/>\n+23 jumps to line 23<br \/>\n&#8211; make vim receive the data from the pipe<\/p>\n<p>zcat \/usr\/share\/man\/man1\/grep.1.gz | grep &#8220;color&#8221;<\/p>\n<p>Search gzipped files<br \/>\nThis decompresses the file and sends the output to STDOUT so it can be grepped. A good one to put in loops for searching directories of gzipped files, such as man pages.<\/p>\n<p>#!\/bin\/sh<br \/>\nSTAMP=`date &#8216;+%Y%m%d-%H:%M&#8217;`<br \/>\nREMOTE_MYCNF=\/var\/log\/mysoft\/mysoft.log<br \/>\nREMOTE_GZ=\/var\/log\/mysoft\/mysoft.log.1.gz<br \/>\nREMOTE_DIR=\/var\/log\/mysoft\/<br \/>\nBACKUP_DIR=\/home\/dev\/logs\/<br \/>\nNEWLOG=&#8221;foo-temp.log&#8221;<br \/>\nssh $1 &#8220;zcat $REMOTE_GZ >> $REMOTE_DIR$NEWLOG&#8221;<br \/>\nssh $1 &#8220;cat $REMOTE_MYCNF >> $REMOTE_DIR$NEWLOG&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to display the contents of a gzip\/gz file By Alvin Alexander. Last updated: Aug 6, 2011 Problem: You have a plain text file that has been compressed with the gzip command, and you&#8217;d like to display the file contents with the Unix\/Linux cat or more commands.<\/p>\n<p>Solution: Instead of using the cat or more [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3427"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3427"}],"version-history":[{"count":4,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3427\/revisions"}],"predecessor-version":[{"id":3431,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3427\/revisions\/3431"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}