Linux / Unix Find Command Avoid Permission Denied Messages

See all Bash/Shell scripting related FAQ
When I type find . -type d -name "foo" command I get Permission denied error messages. How do I exclude all “permission denied: messages from the find command under Linux or Unix like operating systems?

The find command is used to locate files on a Linux or Unix like operating system. The find command will search directory to match the supplied search criteria. You can search for files by type, name, owner, group, date, permissions and more. By default the find will search all subdirectories for you. Let us see how to hide and fix permission denied message when using the find on Linux or Unix-like system.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix terminal
Category File Management
Prerequisites find command
OS compatibility BSD Linux macOS Unix
Est. reading time 3 minutes
Advertisement

Find command basic syntax

The syntax is as follows:
$ find where-to-look criteria action
$ find /dir/to/search -name filetosearch
$ find /dir/to/search -name "*.c"
$ find /home/nixcraft/project/ -name "*.py" -print

In this example, find will search the /tmp directory for any files named “data*.txt” and display their pathnames:
$ find /path/to/dir -name "pattern" -print
$ find /tmp -iname "data*.txt"

OR
$ cd /tmp
$ find . -iname "data*.txt" -print

Fig. 01:  Find will show an error message for each directory on which you don't have read permission.

Fig. 01: Find will show an error message for each directory on which you don’t have read permission.

How to hide or fix find command permission denied messages

In this above example, I do not have read permission for vmware-root and orbit-Debian-gdm directories. To to avoid this problem try the following syntax:
## redirect error spam message to /dev/null ##
$ find where-to-look criteria action 2>/dev/null
$ find . -iname "data*.txt" -print 2>/dev/null

Sample outputs without permission denied spam from find command:

./rtzip/data005.txt
./rtzip/data001.txt
./rtzip/data004.txt
./rtzip/data003.txt
./rtzip/data002.txt
./rtzip/data008.txt
./rtzip/data006.txt
./rtzip/data007.txt
./rtzip/data009.txt

How does it works?

The 2>/dev/null at the end of the find command tells your shell to redirect the error messages (FD #2) to /dev/null, so you don’t have to see them on screen. Use /dev/null to to send any unwanted output from program/command. All data written on a /dev/null special file is discarded by the system. To redirect standard error to /dev/null and store file list to output.txt, type:
## redirect error spam messages to /dev/null ##
$ find . -iname "data*.txt" -print 2>/dev/null > output.txt
$ cat output.txt

Exclude all “permission denied” messages from “find” command on Linux

There is one problem with the following command. It would filter out all error messages created by find command, not just the permission denied ones:
$ find / -name foo 2>/dev/null
$ find / -type d -name bar 2>/dev/null

To avoid that try the following find command along with grep command on Linux or Unix-like systems:
$ find / -name foo 2>&1 | grep -v "Permission denied"
$ find / -type d -name bar 2>&1 | grep -v "Permission denied"

In short you should use following syntax to skip “permission denied” errors messages when running find in Linux or Unix-based systems:
$ find /path/to/dir -name "search-patter" 2>&1 | grep -v "Permission denied"
$ find /etc -name "x*.conf" 2>&1 | grep -v "Permission denied"

To store output to a file run:
$ find /path/to/dir -name "search-patter" 2>&1 | grep -v "Permission denied" > output-file
$ find /etc -name "x*.conf" 2>&1 | grep -v "Permission denied" > output.txt

Display output.txt using cat command:
$ cat output.txt
Linux Find Command Avoid Permission Denied Messages
In the above example, we used find command along with grep command to filter out permission denied error messages.

Conclusion

You learned how to hide and fix permission denied messages when using the find command on your Linux, Unix, or macOS-based systems. Of course, we can also run the command as sudo when possible but avoid all this mess. Unfortunately, you will not get sudo or root access at all times. Hence, we talked about various methods here. For your ready references, sudo syntax would be as follows:
$ sudo find /dir/to/search -name "pattern" -action
$ sudo find / -name "jail.conf" -print

Please see find/bash command man page online or read it by typing the following man command. For example:
$ man find
$ man bash
$ man zsh
$ man ksh

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

10 comments… add one
  • Madan Dec 16, 2014 @ 10:09

    Both errors and warning are thrown into stderr is there any way we can identify that the command has a warning in it and not the error ??
    Apart from checking the return code !!

  • David Feb 17, 2021 @ 9:58

    Just use sudo (assuming you’re not running it in a script)

    example:
    sudo find /* -type f -name

  • Ama Sep 19, 2022 @ 9:21

    Same thing, but with awk instead of grep

    find /dir/to/search -name "pattern" 2>&1  | awk '$NF!~/denied$/'
    • 🛡️ Vivek Gite (Author and Admin) Vivek Gite Sep 19, 2022 @ 10:42

      That is cool. Thanks for sharing it.

  • Anonymous Oct 2, 2022 @ 14:26

    Nice :)

  • JELaBarre Oct 4, 2022 @ 21:37

    I don’t get why this isn’t a switch within ‘find’ itself. Grep can be told to ignore files it can’t read.

  • JC Feb 12, 2023 @ 10:56

    Perfect. Thank you.

  • A Feb 22, 2023 @ 14:30

    Hi,
    Sorry, but I did not find this very informative.
    Indeed the redirection of standard error is a shell trick, not a find trick.
    Your solution does not apply to a shell like tcsh with does not have the 2> redirection capability.
    Same trick would apply to any command, provided you use bash or similar shell.
    I was expecting something like the -s option in grep….
    Best, A.

    • 🛡️ Vivek Gite (Author and Admin) Vivek Gite Feb 22, 2023 @ 19:07

      Yes, tcsh/csh is a bit of pain in butt.

  • Fernando Apr 25, 2023 @ 13:25

    Nice!
    Exactly what I needed.
    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.