I need to search a folder of gzipped files for a string, however I need to search starting at the bottom of each file. I then need the command to return results when it has 10 matches.
So far I have this:
find '/tmp/myfiles/' -type f ( -iname '*.txt' -o -iname '*.gz' ) -exec zcat -f {} + 2>&1 | tac | zgrep -ish 'foobar' | tail -n +1 | head -n 10
I’m using zcat
to get all the file contents, and then I use tac
to reverse them. This is significantly slower than this:
find '/tmp/myfiles/' -type f ( -iname '*.txt' -o -iname '*.gz' ) -exec zgrep -ish 'foobar' {} + 2>&1 | tail -n +1 | head -n 10
However this only reads from the top of the file, I’d like a way to search in reverse without having to read the full file first…
Advertisement
Answer
There is no way to avoid reading the beginning of the file. But reversing the entire file just to get the last matches is obviously significantly slower than reversing just the matches (unless every line contains a match, of course). Maybe try this;
find '/tmp/myfiles/' -type f ( -iname '*.txt' -o -iname '*.gz' ) -exec zgrep -ish 'foobar' {} + 2>&1 | tac | head -n 10