FreeBSD flag to ignore permission errors when searching file for a string

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

shambl3

Posted 2009-09-18T10:54:25.830

Reputation:

Answers

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.

user53528

Posted 2009-09-18T10:54:25.830

Reputation:

The default shell on FreeBSD is tcsh, which doesn't recognize 2>. – Roland Smith – 2014-12-28T15:13:51.360

Thanks however for me it didn't work.

I found the following to work: grep -Flr --include "*" 'mystring' /modules/ | grep -v Perm – None – 2009-09-18T11:09:20.793

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.

Roland Smith

Posted 2009-09-18T10:54:25.830

Reputation: 1 712