我使用以下命令在目录结构中递归查找字符串。
find . -exec grep -l samplestring {} \;
但是当我在一个大型目录结构中运行命令时,会有一长串的
grep: ./xxxx/xxxxx_yy/eee: Is a directory
grep: ./xxxx/xxxxx_yy/eee/local: Is a directory
grep: ./xxxx/xxxxx_yy/eee/lib: Is a directory
我想省略上面的结果。只需显示带有字符串的文件名即可。有人可以帮忙吗?
答案 0 :(得分:6)
每当你说find .
时,该实用程序将返回当前目录结构中的所有元素:文件,目录,链接......
如果你只想找到文件,就这么说吧!
find . -type f -exec grep -l samplestring {} \;
# ^^^^^^^
但是,您可能希望查找包含字符串的所有文件:
grep -lR "samplestring"
答案 1 :(得分:5)
grep -s
或grep --no-messages
如果你希望在多个地方使用这个代码,那么值得阅读the GNU grep documentation中的可移植性说明:
-s
--no-messages
Suppress error messages about nonexistent or unreadable files. Portability note: unlike GNU grep, 7th Edition Unix grep did not conform to POSIX, because it lacked -q and its -s option behaved like GNU grep’s -q option.1 USG-style grep also lacked -q but its -s option behaved like GNU grep’s. Portable shell scripts should avoid both -q and -s and should redirect standard and error output to /dev/null instead. (-s is specified by POSIX.)