Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 38 down vote accepted

You missed a ; 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.

share|improve this answer
5  
If you use the \; ending construct grep is passed one file at a time, so it doesn't display the file name by default, only the matched lines. To get a file list instead add use grep -ls inside of the find construct. –  Caleb May 10 '11 at 6:55

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
share|improve this answer
6  
grep will choke if the expansion goes over ARG_MAX. -R will visit everything while using find one can more easily add primitives to exclude certain files (-name, etc) or not even visit subtrees (-prune). –  Mel May 9 '11 at 23:58
    
Good points @Mel. My point was that in all likelihood the asking party was making things more complex than they needed to be by introducing 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 6:53
2  
@Mel grep does not choke in such a case, exec does. –  Chris Down Mar 26 '13 at 6:21
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.

share|improve this answer
5  
The problem with 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 May 10 '11 at 6:31
    
@Gilles You can get around that problem by using something like 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
    
For completeness we should also mention the -i option for case insensitivity with 'grep'. Also there is -iname in find for case insensitivity. –  Mathew May 10 '11 at 12:01
2  
@Caleb: The only 100% reliable way to have xargs cope with Linux filenames is find ... -print0 | xargs -0, using NUL as separator. Alternative - xargs -d '\n' using newline as separator, 99% reliability. –  grawity May 10 '11 at 20:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.