Find Command
Permission Denied
Linux Troubleshooting
Command Line Tips
Unix Commands

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.

Browse interview questions

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.

bash
1# Suppress all errors (simplest and most common)
2find / -name "myfile.txt" 2>/dev/null
3
4# Suppress only "Permission denied" errors, keep everything else
5find / -name "myfile.txt" 2>&1 | grep -v "Permission denied"

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:

bash
find /var -type f -name "*.log" 2>/dev/null

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:

bash
find / -name "*.conf" 2>&1 | grep -v "Permission denied"

Breaking this down:

  • 2>&1 merges 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:

bash
find / -name "*.conf" 2> >(grep -v "Permission denied" >&2)

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:

bash
1# Instead of searching the entire filesystem
2find / -name "*.log" 2>/dev/null
3
4# Search only where the file is likely to be
5find /var/log -name "*.log"

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:

bash
1find / \
2  -path /proc -prune -o \
3  -path /sys -prune -o \
4  -path /run -prune -o \
5  -name "*.conf" -print 2>/dev/null

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:

bash
find / -readable -name "*.conf" 2>/dev/null

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

MethodSyntaxHides All ErrorsPortablePerformance Impact
Redirect stderr2>/dev/nullYesYesNone
grep -v filter2>&1 | grep -v "Permission denied"No (selective)YesMinor (pipe overhead)
Process substitution2> >(grep -v "Permission denied" >&2)No (selective)Bash onlyMinor
Prune directories-path /proc -prune -oNoYesPositive (skips I/O)
-readable predicate-readableNoGNU onlySlight filter cost

Combining Techniques for Production Scripts

In shell scripts where reliability matters, combine pruning with stderr redirection and explicit -print:

bash
1#!/bin/bash
2# Find all config files outside virtual filesystems
3find / \
4  -path /proc -prune -o \
5  -path /sys -prune -o \
6  -path /dev -prune -o \
7  -path /run -prune -o \
8  -type f -name "*.conf" -print \
9  2>/dev/null

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/null is 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 -readable predicate 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.