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!
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.
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.
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.
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" {}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With