如何使用egrep查找包含字符串的文件 [英] how to find files containing a string using egrep
问题描述
我想在linux下找到包含特定字符串的文件。
我试过类似的东西,但不能成功:
find。 -name * .txt | egrep mystring
这里是发送文件名( find命令的输出
)作为egrep的输入;你真的想在文件的内容上运行egrep。
这里有几个选择:
find。 -name* .txt-exec egrep mystring {} \;
甚至更好
找到。 -name* .txt-print0 | xargs -0 egrep mystring
检查找到命令帮助来检查单个参数的作用。
第一种方法会为每个文件产生一个新的进程,而第二个将传递多个文件作为egrep的参数;需要使用-print0和-0标志来处理可能令人讨厌的文件名(例如,即使文件名包含空格,也可以正确地分隔文件名)。
I would like to find the files containing specific string under linux. I tried something like but could not succeed:
find . -name *.txt | egrep mystring
Here you are sending the file names (output of the find command
) as input to egrep; you actually want to run egrep on the contents of the files.
Here are a couple of alternatives:
find . -name "*.txt" -exec egrep mystring {} \;
or even better
find . -name "*.txt" -print0 | xargs -0 egrep mystring
Check the find command help to check what the single arguments do.
The first approach will spawn a new process for every file, while the second will pass more than one file as argument to egrep; the -print0 and -0 flags are needed to deal with potentially nasty file names (allowing to separate file names correctly even if a file name contains a space, for example).
这篇关于如何使用egrep查找包含字符串的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!