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.
grep -ls
inside of the find construct.
find . -exec grep foo {} +
will show you output like this ./dir/file.py:from foo import bar
find . -exec grep foo {} \;
will show you output like this from foo import bar
find . -exec grep -l foo {} +
will show you output like this ./dir/file.py
find . -exec grep -l foo {} \;
will show you output like this ./dir/file.py
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
when grep
could do the job, but in some cases it would be more effective to to use find to fine tine the file list before going out to grep.
find -exec
. exec
here refers to libc functions which directly call the execve
syscall (eg. exec{ve,vp,lp,...}
), not find -exec
which does its own manipulation prior. The point is that grep
never even sees the arguments to choke on them, it's the kernel that rejects it.
Commented
May 4, 2020 at 15:31
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.
xargs
is that it expects its input to be quoted in a peculiar way that find
doesn't produce. So find … | xargs …
doesn't work if you have file names containing whitespace or \'"
.
Commented
May 10, 2011 at 6:31
find . | xargs -n1 -iX grep "X" 'chrome'
so that arguments are fed one at a time and quoted. Obviously this is a horribly inefficient way to handle this example, but for some situations it's nice.
xargs
cope with Linux filenames is find ... -print0 | xargs -0
, using NUL as separator. Alternative - xargs -d '\n'
using newline as separator, 99% reliability.
Commented
May 10, 2011 at 20:18
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"
find . -name "*.py" -exec fgrep -l hello {} \;
-- it'll print the filenames of files that match, and nothing else
To see list of files instead of lines:
grep -l "chrome" *
or:
grep -r -l "chrome" .
find: Only one instance of {} is supported with -exec ... +
-exec
withfind