Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I grep both compressed files and uncompressed files using the same command

I am trying to make a script for grepping log files located in /var/log/. I am able to either grep uncompressed logs or compressed logs (as a result of log rotation) but have not been able to do both in one command (i get "Binary file (standard input) matches" when I try to do so), is it even possible to do such a thing? thanks!

like image 412
lacrosse1991 Avatar asked Feb 18 '13 18:02

lacrosse1991


People also ask

How do I grep multiple files?

To search multiple files with the grep command, insert the filenames you want to search, separated with a space character. The terminal prints the name of every file that contains the matching lines, and the actual lines that include the required string of characters. You can append as many filenames as needed.

How do I grep a compressed file in Unix?

You need to use zgrep command which invokes grep on compressed or gzipped files. All options specified are passed directly to the grep command or egrep command.

What is the difference between grep and zgrep?

grep states: the exit status is 0 if selected lines are found and 1 otherwise. 2 if an error occurred. zgrep states it works the same as grep, but can handle zipped files. zgrep "hello" * however returns 1 even though the pattern was found in test1.


1 Answers

xzgrep is a one stop shop for commonly compressed files, at least on Ubuntu 16 and macOS 10.12 (installed with xz from MacPorts). From the man page: xzgrep invokes grep(1) on files which may be either uncompressed or compressed with xz(1), lzma(1), gzip(1), or bzip2(1).

Usage (note -r is not supported):

$ find . -type f | parallel -j4 'xzgrep -Hn "PATTERN" {}'

like image 188
Rob Avatar answered Nov 09 '22 00:11

Rob