0

I am trying to pipe find into grep with the negative match option but I don't get the desired results. I am using the Ubuntu subshell for Windows and running the command:

find / -name CCP.cf | grep -v '*denied'

This produces the output (a lot omitted for brevity):

find: ‘/mnt/c/Config.Msi’: Permission denied
find: ‘/mnt/c/Intel/IntelOptaneData’: Permission denied
find: ‘/mnt/c/Program Files/WindowsApps’: Permission denied

Is this printout from the find and can it not be directed to grep? Essentially I want to be able to use find and it return the directory of the file to me (I'm going to wrap it into a function later).

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Chilly
  • 25
  • 5
  • You're grepping the filenames, not the contents of the files. Is that what you want? – Barmar Feb 27 '20 at 02:07
  • `*denied` is not a valid regular expression. `*` needs a pattern before it. – Barmar Feb 27 '20 at 02:07
  • That output is from `find`, it's just telling you that there are some directories that it can't search because you don't have permission to read those directories. – Barmar Feb 27 '20 at 02:08

1 Answers1

3

The error messages go to standard error, but you're only piping standard output to grep. To redirect stderr as well, use 2>&1, this redirects stderr (FD 1) to stdout (FD 1), which has already been redirected to the pipe.

find / -name CCP.cf 2>&1 | grep -v 'Permission denied'

You could also just filter out all error messages:

find / -name CCP.cf 2>/dev/null
Barmar
  • 741,623
  • 53
  • 500
  • 612