一键搜索压缩*和*非压缩文件

Max*_*ams 3 linux zip grep gzip

我有一个文件夹,其中包含要搜索的压缩和解压缩日志文件。

我可以搜索未压缩的

grep -r "my-search-string" /path/to/folder
Run Code Online (Sandbox Code Playgroud)

并搜索压缩文件

find /path/to/folder -name "*.gz" -exec zcat "{}" + | grep "my-search-string"
Run Code Online (Sandbox Code Playgroud)

是否有一个单行我可以用来搜索所有压缩和解压缩的文件/path/to/folder

谢谢,马克斯

小智 5

zgrep 是您的朋友!

   Zgrep  invokes grep on compressed or gzipped files.  All options speci?
   fied are passed directly to grep.  If no file is  specified,  then  the
   standard input is decompressed if necessary and fed to grep.  Otherwise
   the given files are uncompressed if necessary and fed to grep.
Run Code Online (Sandbox Code Playgroud)

我只是注意到一些 zgrep 版本没有 -r 选项。在这种情况下,您可以使用查找模式来捕获所有文件并使用 zgrep 而不是 grep,例如:

找 。-type f -exec zgrep '我的搜索字符串' "{}" \;