OSETC TECH

How to Exclude All “Permission denied” messages When Using Find Command

This post will guide you how to avoid “permission denied” spam when using find command at the shell prompt in your Linux system. How do I only exlude “permission denied” error message from the outputs of find command under Linux operating systems.

Find Command


Find Command searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence (see section OPERATORS), until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name.

The Syntax of the find command is as followss:

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

For example, you wish to search files called “passwd” in the /etc/directory, and you can type the following command:

$ find /etc/ -name "passwd"

Outputs:

[devops@localhost ~]$ find /etc/ -name "passwd"
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/pki/CA/private’: Permission denied
find: ‘/etc/pki/rsyslog’: Permission denied
/etc/passwd
find: ‘/etc/selinux/targeted/active’: Permission denied
find: ‘/etc/selinux/targeted/tmp’: Permission denied
find: ‘/etc/selinux/final’: Permission denied
find: ‘/etc/dhcp’: Permission denied
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/lvm/cache’: Permission denied
/etc/pam.d/passwd
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/firewalld’: Permission denied
find: ‘/etc/vmware-tools/GuestProxyData/trusted’: Permission denied
find: ‘/etc/audisp’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/ipsec.d’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/ntp/crypto’: Permission denied
find: ‘/etc/sssd’: Permission denied

From the above outputs, you will see that find command will show an “permission denied” error message when your current user do not have read permission for those files or directories.

Excluding Permission Denied Messages only


If you wish to exclude all Permission denied messages when using find command, and you can do redirect all stderr output(2>) to /dev/null, then you will not see any error message from your standard output. type:

$ find /etc/ -name "passwd" 2>/dev/null

Outputs:

[devops@localhost ~]$ find /etc/ -name "passwd" 2>/dev/null
/etc/passwd
/etc/pam.d/passwd

As the above command will filter out all error message, this is not a good solution. If you only want to filter out all “permission denied” error message, and you need to combine with grep command with -v option. type:

$ find /etc/ -name "passwd" 2>&1 | grep -v "Permission denied"

Outputs:

[devops@localhost ~]$ find /etc/ -name "passwd" 2>&1 | grep -v "Permission denied"
/etc/passwd
/etc/pam.d/passwd

Conclusion


You should know that how to exclude all “permission denied” error messages when running Find command at the shell prompt under CentOS or RHEL or Ubuntu Linux Operating systems.