I'd like to find the files in the current directory that contain the text "chrome".
$ find . -exec grep chrome
find: missing argument to `-exec'
What am I doing wrong?
I'd like to find the files in the current directory that contain the text "chrome".
$ find . -exec grep chrome
find: missing argument to `-exec'
What am I doing wrong?
You missed a ; (escaped here as \; to prevent the shell from interpreting it) or a + and a {}:
find . -exec grep chrome {} \;
or
find . -exec grep chrome {} +
find will execute grep and will substitute {} with the filename(s) found. The difference between ; and + is that with ; a single grep command for each file is executed whereas with + as many files as possible are given as parameters to grep at once.
You don't need to use find for this at all; grep is able to handle opening the files either from a glob list of everything in the current directory:
grep chrome *
...or even recursively for folder and everything under it:
grep chrome . -R
find . | xargs grep 'chrome'
you can also do:
find . | xargs grep 'chrome' -ls
The first shows you the lines in the files, the second just lists the files.
Caleb's option is neater, fewer keystrokes.
Find is one way and you can try the_silver_searcher then all you need to do is
ag chrome
It will search chrome in all files (include sub directories) and it is faster than find
Here's an example of how I usually use find/exec...
find . -name "*.py" -print -exec fgrep hello {} \;
This searches recursively for all .py files, and for each file print(s) out the filename and fgrep's for 'hello' on that (for each) file. Output looks like (just ran one today):
./r1.py
./cgi-bin/tst1.py
print "hello"
./app/__init__.py
./app/views.py
./app/flask1.py
./run.py
./tst2.py
print "hello again"
To see list of files instead of lines:
grep -l "chrome" *
or:
grep -r -l "chrome" .