Find a file by name in Linux and don't show permission denied
Published by Nicholas Dunbar on October 10th, 2012
Looks for a file named "game" starting at the root directory (searching all directories including mounted filesystems). The `-name' option makes the search case sensitive. You can use the `-iname' option to find something regardless of case.
find / -name game
don't show lines with "Permission denied"
find / -name game |& grep -v "Permission denied"
or
find / -name game 2>/dev/null
or
sudo find / -name game
Keep in mind that the second solution will not only eliminate the permission errors but eliminate all errors returned from the find command. Hence why we suggest the grep as a way to explicitly remove only the "Permission denied" errors.
Note: Using the pipe before directing stderr to /dev/null will not work.
Example:
find / -name *game | grep -E "^mario_" 2>/dev/null
lets go over why this will not work:
find / -name *game creates a list of files that end in the word game and routs that list to 1> (stdout) it takes the erros and routes them to 2> (std err), but instead of using 1> or 2> like this:
find / -name *game 1> echo
we are using the pipe (|) which puts 1 and 2 together. So after the | grep -E "^mario_" is receiving 1 and 2 together as one stream. After it does it's processing it outputs one stream where stderror and stdout are no longer seperated. Hence when 2>/dev/null is ran the permission errors will no longer be in 2 and so they will not get diverted to /dev/null.