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?
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?
Three ways come to mind:
sudo find / -name "whatever" -print
find / -name "whatever" -print 2>/dev/null
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
}
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
sudo
does not prevent these error messages on macOS v13 :/ – Phrogz Sep 28 '23 at 14:02