Why does this command

find /etc -exec grep student {} \; 2>/dev/null

show me more results than this command

grep -r student /etc 2>/dev/null

terminal window

  • Add -H to the grep command in the find run string: this will show you the file names, and thereby give you a better idea of what is happening, whether more files are found, or more matches in each file. It looks as though the find command is finding a dictionary, not found by grep -r. – AFH May 24 '18 at 19:14
  • Thank you AFH for the -H explanation will definitely use it in the future. Thank you Kamil, -R instead of -r has solved the problem. Nope my grep wasn't an alias. – Tomas.R May 24 '18 at 19:33

Probably there are some symbolic links under your /etc. It looks like your grep -r doesn't follow them but find does.

Try grep -R.

Note: neither -r nor -R is required by POSIX. Some implementations of grep may not support them; some may support one of them, not necessarily following (or not) symbolic links like in this example; some may treat -R and -r alike. Refer to man 1 grep in your OS to be sure.

  • I was puzzled why find with its default -P was returning links, but I now realise that that is precisely what it does: it returns a link, but this is passed to grep, which follows the link when reading the file, whereas grep -r ignores links, whether to files or directories. Both commands will return the same search results if -type f is added to the find run string. This may be obvious, but it took me some time to understand, so I thought it worth noting. Also, note that, if there are directory links, grep -R will check more files than the find command unless -L is added. – AFH May 24 '18 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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