在一次点击中搜索压缩的*和*非压缩文件


2

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

我可以搜索非压缩的

grep -r "my-search-string" /path/to/folder

并使用搜索压缩文件

find /path/to/folder -name "*.gz" -exec zcat "{}" + | grep "my-search-string"

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

谢谢,马克斯

Answers:


4

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.

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

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


1

为'my-search-string'和路径导出一些有用的变量,然后搜索:

SEARCH='my-search-string' ; SEARCHPATH='/path/to/folder' ; grep -r "$SEARCH" $SEARCHPATH && find $SEARCHPATH -name "*.gz" -exec zcat "{}" + | grep "$SEARCH"
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.