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.
– Caleb
May 10 '11 at 06:55
find . -exec grep foo {} +
will show you output like this ./dir/file.py:from foo import bar
– s g
Apr 17 '15 at 20:30
find . -exec grep foo {} \;
will show you output like this from foo import bar
– s g
Apr 17 '15 at 20:31
find . -exec grep -l foo {} +
will show you output like this ./dir/file.py
– s g
Apr 17 '15 at 20:32
find . -exec grep -l foo {} \;
will show you output like this ./dir/file.py
– s g
Apr 17 '15 at 20:33
find . -print -exec grep chrome \;
to have find
print out the file name...
– miken32
Jan 27 '17 at 17:48
find . -type f -execdir grep foo {} +
will show you output like this ./file.py:from foo import bar
– Mark Booth
Aug 16 '17 at 09:21
file ./ -exec grep -H chrome {} \;
– Svend Hansen
Oct 20 '17 at 09:28
fish
shell you need to escape the braces too find . -exec grep chrome \{\} \;
– Tamlyn
Dec 08 '17 at 15:33
find
and just use grep
: grep -ls 'chrome' *
– flow2k
Sep 14 '18 at 06:34
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.
– Caleb
May 10 '11 at 06:53
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.
– Chris Down
May 04 '20 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 \'"
.
– Gilles 'SO- stop being evil'
May 10 '11 at 06: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.
– Caleb
May 10 '11 at 11:56
xargs
cope with Linux filenames is find ... -print0 | xargs -0
, using NUL as separator. Alternative - xargs -d '\n'
using newline as separator, 99% reliability.
– u1686_grawity
May 10 '11 at 20:18
find . -print0 | xargs -I {} grep 'chrome' {}
which is roughly equivalent to the -exec
– Bryan Sep 09 '15 at 15:00Find 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
– Jeff Schaller
Aug 23 '17 at 16:19
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 ... +
– Costa Michailidis Apr 03 '17 at 19:36-exec
withfind
– Kusalananda Jun 22 '18 at 06:22