我想在当前目录中找到包含文本“chrome”的文件。
$ find . -exec grep chrome
find: missing argument to `-exec'
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
小智 1016
您错过了 a ;(在此处转义\;以防止 shell 对其进行解释)或 a+和 a {}:
find . -exec grep chrome {} \;
Run Code Online (Sandbox Code Playgroud)
或者
find . -exec grep chrome {} +
Run Code Online (Sandbox Code Playgroud)
find将执行grep并替换{}为找到的文件名。之间的差;和+是与;单个grep每个文件命令而用执行+尽可能多的文件尽可能给出作为参数传递给grep在一次。
Cal*_*leb 61
您根本不需要为此使用find;grep 能够处理从当前目录中所有内容的全局列表中打开文件:
grep chrome *
Run Code Online (Sandbox Code Playgroud)
...甚至递归文件夹及其下的所有内容:
grep chrome . -R
Run Code Online (Sandbox Code Playgroud)
小智 19
find . | xargs grep 'chrome'
Run Code Online (Sandbox Code Playgroud)
你也可以这样做:
find . | xargs grep 'chrome' -ls
Run Code Online (Sandbox Code Playgroud)
第一个显示文件中的行,第二个只列出文件。
Caleb 的选择更简洁,击键次数更少。
Ask*_*arn 10
查找是一种方法,您可以尝试,the_silver_searcher然后您需要做的就是
ag chrome
Run Code Online (Sandbox Code Playgroud)
它将在所有文件(包括子目录)中搜索 chrome,并且比 find 更快
小智 6
这是我通常如何使用 find/exec 的示例...
find . -name "*.py" -print -exec fgrep hello {} \;
Run Code Online (Sandbox Code Playgroud)
这将递归搜索所有 .py 文件,并为每个文件打印出文件名和 fgrep 在该(每个)文件上的“hello”。输出看起来像(今天刚运行了一个):
./r1.py
./cgi-bin/tst1.py
print "hello"
./app/__init__.py
./app/views.py
./app/flask1.py
./run.py
./tst2.py
print "hello again"
Run Code Online (Sandbox Code Playgroud)
小智 5
要查看文件列表而不是行:
grep -l "chrome" *
Run Code Online (Sandbox Code Playgroud)
或者:
grep -r -l "chrome" .
Run Code Online (Sandbox Code Playgroud)