Apple - How do I prevent the permissions denied message from being displayed when I do a find command?
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.
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
}