我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
当前回答
我认为值得提到你如何找到:
所有包含至少一个文本的文件,其中包括大量的文本:
grep -rlf ../patternsFile.txt .
出口:
./file1
./file2
./file4
上述,由每个文本组成:
cat ../patternsFile.txt | xargs -I{} sh -c "echo {}; grep -rl \"{}\" ."
出口:
pattern1
./file1
./file2
pattern2
./file1
./file4
pattern3
./file1
./file2
./file4
请注意,为了不匹配模式File.txt本身,您需要添加一个目录(如上面的示例所示)。
其他回答
要搜索字符串和输出,就是与搜索字符串相同的字符串:
for i in $(find /path/of/target/directory -type f); do grep -i "the string to look for" "$i"; done
吉:
for i in $(find /usr/share/applications -type f); \
do grep -i "web browser" "$i"; done
要显示包含搜索字符串的文件名:
for i in $(find /path/of/target/directory -type f); do if grep -i "the string to look for" "$i" > /dev/null; then echo "$i"; fi; done;
吉:
for i in $(find /usr/share/applications -type f); \
do if grep -i "web browser" "$i" > /dev/null; then echo "$i"; \
fi; done;
grep -insr "pattern" *
i: 在 PATTERN 和输入文件中忽略案例区别. n: 在输入文件中预定每个输出线的 1 行号. s: 删除关于不存在或不可读的文件的错误消息. r: 阅读每个目录下的所有文件,重复。
重复和案例不敏感的捕捉与线号:
grep -inr "Text" folder/to/be/searched/
您可以使用捕捉工具重复搜索当前文件夹,如:
grep -r "class foo" .
注意: -r - 重复搜索分支机构。
grep "class foo" **/*.c
如果你有错误,你的论点太长,考虑缩短你的搜索,或者使用找到合成代替,如:
find . -name "*.php" -execdir grep -nH --color=auto foo {} ';'
或者使用Ripgrep。
rg "class foo" .
它比任何其他工具,如 GNU/BSD grep, ucg, ag, sift, ack, pt 或类似,因为它是建立在Rust的 regex 引擎的顶部,使用终端自动化,SIMD和攻击性字体优化,使搜索非常快。
-i - 不敏感的搜索. -I - 忽略二进制文件. -w - 搜索完整的单词(相反的部分单词匹配)。 -n - 显示您的匹配线. -C/--背景(例如 -C5) - 增加背景,所以你看到周围的代码. --color=auto - 标记匹配文本. -H - 显示文件名,在文本中找到。
试试这
find . -type f -name some_file_name.xml -exec grep -H PUT_YOUR_STRING_HERE {} \;