How can I exclude all permission denied messages from find?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Redirect stderr to /dev/null by appending 2>/dev/null to your find command. This suppresses all error messages, including "Permission denied." If you need to suppress only permission errors while preserving other error output, pipe stderr through grep -v instead.
Both approaches work on Linux, macOS, and any POSIX-compliant shell. The right choice depends on whether you care about non-permission errors.
Why find Produces "Permission Denied" Messages
The find command walks the directory tree recursively. When it encounters a directory the current user lacks read permission on, it cannot list that directory's contents. Instead of silently skipping it, find writes a diagnostic message to stderr (file descriptor 2) and continues traversing the rest of the tree.
This behavior is intentional. The error tells you the search was incomplete because certain directories were inaccessible. On a typical Linux system, searching from / as a non-root user generates dozens of these messages from directories like /root, /proc, /sys, and various system service directories.
Method 1: Redirect All of stderr
The most widely used technique discards everything written to stderr:
The 2> syntax redirects file descriptor 2 (stderr) to the target. /dev/null is a special file that discards all data written to it.
This is the right default when your goal is clean output and you are confident about the search path.
Method 2: Filter Only Permission Errors
When you want to see other errors (broken symlinks, I/O errors, filesystem issues) but hide only the permission noise, redirect stderr into stdout and filter:
Breaking this down:
2>&1merges stderr into stdout so both streams flow through the pipe.grep -v "Permission denied"removes any line containing that string.
The tradeoff is that the merge makes it impossible to separate results from errors afterward in the same pipeline. Any downstream tool receives a single mixed stream.
A cleaner alternative uses process substitution (Bash only) to filter stderr without merging it into stdout:
This keeps stdout clean for piping into other tools while still showing non-permission errors on stderr.
Method 3: Narrow the Search Path
Often the best solution is to avoid the errors entirely by searching a more specific directory:
Narrowing the path is faster, produces fewer irrelevant results, and generates fewer permission errors. Reserve full-system searches for cases where you genuinely do not know where the file lives.
Method 4: Prune Known-Noisy Directories
For system-wide searches where you need broad coverage but want to skip directories that always produce permission errors:
The -prune action prevents find from descending into the specified directories at all. This is more efficient than letting find attempt entry and fail, because it avoids the stat calls entirely.
Method 5: Use -readable (GNU find)
GNU find (default on most Linux distributions) supports the -readable predicate:
This filters results to only include files and directories that the current user can read. However, -readable does not prevent find from attempting to enter unreadable directories during traversal, so you may still see some permission errors. The 2>/dev/null redirect is still advisable as a safety net.
Note that -readable is a GNU extension and is not available on macOS's BSD find or other non-GNU implementations.
Comparison of Approaches
| Method | Syntax | Hides All Errors | Portable | Performance Impact |
| Redirect stderr | 2>/dev/null | Yes | Yes | None |
| grep -v filter | 2>&1 | grep -v "Permission denied" | No (selective) | Yes | Minor (pipe overhead) |
| Process substitution | 2> >(grep -v "Permission denied" >&2) | No (selective) | Bash only | Minor |
| Prune directories | -path /proc -prune -o | No | Yes | Positive (skips I/O) |
| -readable predicate | -readable | No | GNU only | Slight filter cost |
Combining Techniques for Production Scripts
In shell scripts where reliability matters, combine pruning with stderr redirection and explicit -print:
The explicit -print ensures that pruned directories do not appear in the output. Without it, some versions of find print the pruned directory name itself.
Common Pitfalls
Hiding all errors when you only wanted to hide permission errors. Using 2>/dev/null suppresses every error, including broken symlinks, missing mount points, and I/O failures. If your script needs to detect those problems, use the selective grep -v approach instead.
Assuming merged output is still separated. After 2>&1, both stdout and stderr are one stream. Piping that into grep -v filters results and errors alike. If a legitimate search result contains the string "Permission denied" in its filename, it gets removed too.
Forgetting that -readable is GNU-only. Scripts that use -readable break on macOS, FreeBSD, and other systems with BSD find. Stick to 2>/dev/null for cross-platform scripts.
Omitting -print with -prune. The default -print action in find applies to all matched expressions, including the pruned paths. Add an explicit -print after your real predicates to avoid printing pruned directory names.
Equating clean output with complete output. Suppressing errors does not make the search complete. If find could not enter /root, nothing inside /root appears in the results. The file might still be there. For truly complete searches, run find as root with sudo.
Summary
find ... 2>/dev/nullis the standard one-liner for suppressing all errors, including permission denied.2>&1 | grep -v "Permission denied"selectively hides only permission errors when other diagnostics matter.- Narrowing the search directory and pruning virtual filesystems reduce both errors and execution time.
- GNU find's
-readablepredicate filters results but does not fully eliminate traversal errors. - Clean output is not the same as complete output. Hidden permission errors mean parts of the filesystem were not searched.
Related reading
- How can I exclude the conditions evaluation report from the console of a Spring boot application?
- How can I find and run the keytool
- How can I find Java heap size and memory used Linux?
- How can I find out who force pushed in git?
- How can I find out why my storage space on Amazon EC2 is full?
- How can I find the method that called the current method?
- How can I fix a MemoryError when executing scikit-learns silhouette score?
- How can I fix 'android.os.NetworkOnMainThreadException'?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.