4

Im using the following to serch for a string in all the files in a directory.

grep -Flr --include "*" 'mystring' /modules/

Which works perectly fine and returns the files which contain the string. However I also get a list of files with permission denied next to them which means looking for the results a bit harder.

Is there a flag to pass to the command to ignore outputting permission denied errors.

Thanks

2 Answers 2

3

I usually just use:

grep -Flr --include "*" 'mystring' /modules/ 2>/dev/null

which will throw away all output to standard error, assuming you have a shell allowing for 2> standard error redirection. If not, see here.

If (as you seem to indicate in your comment), your version of grep outputs its errors to standard output rather than standard error, you can filter that output with something like:

grep -Flr --include "*" 'mystring' /modules/ | egrep -v 'Permission denied|ERROR'

This will throw away lines based on whatever pattern you deem is necessary.

2
  • Thanks however for me it didn't work. I found the following to work: grep -Flr --include "*" 'mystring' /modules/ | grep -v Perm
    – shambl3
    Sep 18, 2009 at 11:09
  • The default shell on FreeBSD is tcsh, which doesn't recognize 2>. Dec 28, 2014 at 15:13
0

If you are using BSD grep, use the -s flag;

grep -Flrs --include "*" 'mystring' /modules/

Additionally, I think you can drop the --include "*" because BSD grep reads directories by default and recurses over them if you use -r or -R.

You must log in to answer this question.