红联Linux门户
Linux帮助

find与grep

发布时间:2008-08-23 20:47:36来源:红联作者:Travel
在当前目录(包含子目录)下查找包含"Test"字符串的所有cpp和h文件的shell命令

find . -name "*" -exec grep -i "xxxx" {} \; -print

其中,-i是grep的参数,表示忽略大小写

在linux 里man find 里有一段说明:

-exec command ;
Execute command; true if 0 status is returned. All following
arguments to find are taken to be arguments to the command until
an argument consisting of `;' is encountered. The string `{}'
is replaced by the current file name being processed everywhere
it occurs in the arguments to the command, not just in arguments
where it is alone, as in some versions of find. Both of these
constructions might need to be escaped (with a `\') or quoted to
protect them from expansion by the shell. The command is exe-
cuted in the starting directory.

意思是说可以在FIND后调用外部命令;后面出现的所有参数都是该命令的参数直到一个由“;”构成的参数为止。字符串“{}”由当前的正在被处理的文件名所代替。“\”是一个转义符。

例如:find . -name "*.php" -exec grep viewnew {} -H \;

这条命令的意思是,查当前目录以下包括各个子目录中所有PHP文件,并用GREP查代里面含有“viewnew”字样,并把这个文件名显示出来。

另外,要注意的是,在使用时,经常遇到 find: missing argument to `-exec'

这个意思并不是说没有exec参数,而是exec后面的参数不对。这时,应该检查\;之前有没有空格之类。
文章评论

共有 0 条评论