Linux / Unix Find Command: Avoid Permission Denied Messages

by on March 10, 2013 · 0 comments· LAST UPDATED March 10, 2013

in , , ,

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
Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Requirementsfind command+
Unix like os
Estimated completion timeN/A
type, name, owner, group, date, permissions and more. By default the find will search all subdirectories for you.

Find command basic syntax

The syntax is:


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 /tmp -iname "data*.txt"
 

OR

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

Sample outputs:

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.


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 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 to /dev/null ##
find . -iname "data*.txt" -print 2>/dev/null > output.txt
cat output.txt
 
See also
  • Man page for bash or ksh shell.
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 0 comments… add one now }

Leave a Comment

Tagged as: , , , , , , , , , , , ,

Previous Faq:

Next Faq: