23

I'm trying to run a find command, aka:

find / -name "some_file" -print 

and my results are full of permission denied messages:

find: /.DocumentRevisions-V100: Permission denied

How do I prevent them from showing up in my search results?

3 Answers 3

44

Three ways come to mind:

  • run the command as administrator: sudo find / -name "whatever" -print
  • discard all error output: find / -name "whatever" -print 2>/dev/null
  • filter "Permission denied" messages: find / -name "whatever" -print 2>&1 | fgrep -v "Permission denied"

The key difference between the second and third option is probably that the second discards all error messages while the third will not show any files/folders where the name contains "Permission denied" (which is probably highly unlikely).

In addition it may be also worth noting that you shouldn't use the third option if you plan to further process the output of find via a pipe. The reason here is that standard and error output are sent via two different channels (and only visually combined afterwards by the shell). If you pipe the output into another command only the content of standard output will be inputed into the next command.

1
  • Running as sudo does not prevent these error messages on macOS v13 :/
    – Phrogz
    Commented Sep 28, 2023 at 14:02
7

Here is an excellent discussion on this subject.

I finally stuck myself to adding the following function into ~/.bash_profile script:

find() {
  { LC_ALL=C command find -x "$@" 3>&2 2>&1 1>&3 | \
    grep -v -e 'Permission denied' -e 'Operation not permitted' >&3; \
    [ $? = 1 ]; \
  } 3>&2 2>&1
}
2

A bit simpler version of Dmitry's answer:

in ~/.zprofile (macos - specific otherwise ~/.bash_profile)

myfind() {
  find "$@" 2>&1 | grep -v 'Permission denied'
}

Now you can run the find command almost as usual:

myfind / | grep ollama 
/usr/local/bin/ollama
2
  • Why not run myfind / -name ollama directly?
    – nohillside
    Commented Dec 6, 2023 at 18:44
  • you could and you should as it would be faster. The grep was just as an example that the command works as normal find. Dmitry's version for some reason didn't work for me on my mac.
    – Deian
    Commented Dec 7, 2023 at 14:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .