我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
当前回答
要搜索字符串和输出,就是与搜索字符串相同的字符串:
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;
其他回答
你的命令是正确的. 你只需要添加 -l 来抓住:
find / -type f -exec grep -l 'text-to-find-here' {} \;
试试:
find . -name "*.txt" | xargs grep -i "text_pattern"
如果你有一组文件,你会一直在检查,你可以列出他们的路径,例如:
alias fd='find . -type f -regex ".*\.\(inc\|info\|module\|php\|test\|install\|uninstall\)"'
然后你可以简单地过滤列表如下:
grep -U -l $'\015' $(fd)
将列表 fd 过滤到包含 CR 模式的文件。
我发现,将我感兴趣的文件会帮助我创建更容易的脚本,然后总是试图记住如何获得所有这些文件。 回复的物品工作,但早或晚你将不得不争取分散特定的文件类型。
您可以使用下面的命令,因为您不想要文件名,但您想要从所有文件中搜索。
grep -e TEXT *.log | cut -d' ' --complement -s -f1
捕捉与 -e 选项是相当快的与其他选项相比,因为它是PATTERN 比赛
您可以使用以下命令从文件中找到特定文本:
cat file | grep 'abc' | cut -d':' -f2