我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
当前回答
使用 pwd 从您所在的任何目录中搜索,向下滑动
grep -rnw `pwd` -e "pattern"
取决于您正在使用的 grep 版本,您可以忽略 pwd. 在更新的版本中,如果没有目录,则似乎是 grep 的默认情况。
这样:
格雷普 -rnw -e “模式”
或
格雷普 -rnw “模式”
他们会像上面那样做!
其他回答
使用:
grep -c Your_Pattern *
这将报告您的模式在当前目录中的每个文件中有多少副本。
要搜索字符串和输出,就是与搜索字符串相同的字符串:
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;
如果您严格想要使用查找,则使用查找 + 查找:
找到 /path/to/somewhere/ -type f -exec grep -nw 'textPattern' {} \;
步骤:
使用查找搜索文件,执行捕获所有。
这给了你找到文件的权力。
使用 -name 模式,如果您只想捕捉某些文件:
找到 /path/to/somewhere/ -type f -name \*.cpp -exec grep -nw 'textPattern' {} \;
您可以使用不同的搜索选项来改善您的文件搜索。
您可以使用以下命令从文件中找到特定文本:
cat file | grep 'abc' | cut -d':' -f2
我认为值得提到你如何找到:
所有包含至少一个文本的文件,其中包括大量的文本:
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本身,您需要添加一个目录(如上面的示例所示)。