I have dozens of folders which contains both plain text log files and gzipped older logs. My goal is just a one liner to run (one folder at a time) to pull up all grep results based on timestamp order, regardless if the log file containing a match is txt or gz, and if possible to optimize for performance.
This works fine for the plain files:
ls -rt log.*.txt | xargs grep <treasure> -
I use this rather than just grep, so the results are sorted in chronological order of file creation, possibly spanning multiple days, rather than sorted based on the filename. Filenames (log.#.txt) grow to a certain integer limit, then wrap to log.0.txt, but this can cross over the 24hr mark or not.
Once the txt files wrap, the older files are gzipped: log.#.archive.gz. Only integer-limit gz files are kept.
I thought to use an if/fi statement to either grep or zgrep depending on the current file's extension. However, my first step to try it on just the gz files didn't work:
ls -rt log.*.gz | xargs zgrep <treasure> -
I get a bunch of errors "file 'treasure' not found" (one for each .gz file)
I've also tried
ls -rt "log.*.gz" | xargs -0 zgrep <treasure> -
to the same result. I known this must be due to my rudimentary understanding of the xargs command. Maybe I can even accomplish this with proper grep/zgrep options, find, or something else entirely.