shell 脚本:通过在文件夹中的所有文件中搜索来查找字符串?
shell script : find a string by searching inside all the files in a folder?
如何在包含隐藏文件和子文件夹的文件夹中找到(可能是多个)文件中包含的字符串?
我试过这个命令:
find . -maxdepth 1 -name "tes1t" -print0 | sed 's,\.\/,,g'
但是没有结果。
首先,您的 maxdepth
应该是 2
而不是 1
,现在您的 find command won't descend in subdirectories. Furthermore you can simply grep 用于 find
的输出模式.这可以通过以下方式实现:
find . -maxdepth 2 -type f -exec grep 'pattern here' '{}' \;
解释:
find .
在当前目录执行find
。
-maxdepth 2
子目录不再下降。
-type f
查找所有不是目录的文件。
-exec grep 'pattern' '{}'
执行具有特定模式的 grep
语句,{}
包含找到的每个文件的文件名。
为 grep 添加颜色突出显示选项,输出行号 and/or 文件名。
有关详细信息,请参阅 man find
和 man grep
。
grep -Hnr PATTERN .
如果你的grep支持-r
(递归,=-d recurse
)。请注意,那时递归深度将没有限制。
或尝试grep -d skip -Hn PATTERN {,.[!.]}*{,/{,.[!.]}*}
;这应该有效,因为 grep 接受多个文件参数。如果您的 grep
版本不支持,请丢弃 -d skip
内容。对于没有大括号扩展的 shell,使用手动扩展形式 * */* */.[!.]* .[!.]* .[!.]*/* .[!.]*/.[!.]*
.
如何在包含隐藏文件和子文件夹的文件夹中找到(可能是多个)文件中包含的字符串?
我试过这个命令:
find . -maxdepth 1 -name "tes1t" -print0 | sed 's,\.\/,,g'
但是没有结果。
首先,您的 maxdepth
应该是 2
而不是 1
,现在您的 find command won't descend in subdirectories. Furthermore you can simply grep 用于 find
的输出模式.这可以通过以下方式实现:
find . -maxdepth 2 -type f -exec grep 'pattern here' '{}' \;
解释:
find .
在当前目录执行find
。-maxdepth 2
子目录不再下降。-type f
查找所有不是目录的文件。-exec grep 'pattern' '{}'
执行具有特定模式的grep
语句,{}
包含找到的每个文件的文件名。
为 grep 添加颜色突出显示选项,输出行号 and/or 文件名。
有关详细信息,请参阅 man find
和 man grep
。
grep -Hnr PATTERN .
如果你的grep支持-r
(递归,=-d recurse
)。请注意,那时递归深度将没有限制。
或尝试grep -d skip -Hn PATTERN {,.[!.]}*{,/{,.[!.]}*}
;这应该有效,因为 grep 接受多个文件参数。如果您的 grep
版本不支持,请丢弃 -d skip
内容。对于没有大括号扩展的 shell,使用手动扩展形式 * */* */.[!.]* .[!.]* .[!.]*/* .[!.]*/.[!.]*
.