On 2020-08-05 11:30, Bruce Wen wrote: > Hi, > > Sometimes, find command failed due to "Permission denied". > > For example, the file owner is not me, and I have no read permission to the > file, then "Permission denied" happened. > > In this case, -readable and -perm cannot help. So, any way to avoid > "Permission denied"? > > Thanks! > > Br, > Bruce Wen
Unfortunately, you didn't give us the exact command to reproduce the problem. So I assume you tried something like this: # Run as regular user, this would usually give "permission denied" # for e.g. '/root' and other files/directories like '/var/spool/cups'. $ find / -xdev -ls >/dev/null find: '/root': Permission denied ... But the following works for me: pruning unreadable directories: $ find / -xdev -type d ! -readable -prune -o -ls >/dev/null (no errors) So the trick is not to continue when the current file/dir is readable (because this is what find is doing anyway unless other negative test results prevent this), but to stop processing the directory hierarchy when some directory is not readable. I suggest consulting the documentation for the -prune test. FWIW there is currently a bug with the cost-based optimizer [1] which lets other tests like -empty be performed before -readable even if that test is specified later on the command line. [1] https://lists.gnu.org/r/bug-findutils/2020-05/msg00008.html Have a nice day, Berny