Are you facing an error 'ignore permission denied message from find' while running find command in Linux?
This guide will help you.
While using Linux, you may encounter the error, "permission denied".
This error occurs when the user does not have the privileges to make edits to a file. Root has access to all files and folders and can make any edits.
Remember that only root or users with Sudo privileges can change permissions for files and folders.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to fix Linux related errors.
Find command basic syntax
For instance, 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/ibmimedia/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"
find . -iname "data*.txt" -print
How to resolve 'ignore permission denied message from find' ?
To fix this error, apply the tip give below.
Recently, one of our customers was trying to execute the below find command in Linux/Unix and received "Permission denied" error messages.
find . -type d -name “ibmimedia”
So, we can hide or fix find command permission denied messages.
Here is how we do it.
How to hide or fix find command permission denied messages in Linux ?
In the above example, we don't have the read permission for vmware-root and orbit-Debian-gdm directories.
So, we use the below syntax to avoid the problem:
## redirect error spam message to /dev/null ##
find where-to-look criteria action 2>/dev/null
find . -iname "data*.txt" -print 2>/dev/null
Here is the output without permission denied spam from find command:
Here, at the end of the find command 2>/dev/null tells the shell to redirect the error messages (FD #2) to /dev/null, so we don’t have to see them on screen.
We use /dev/null to send any unwanted output from program/command. The system discards all data written on a /dev/null special file.
To redirect standard error to /dev/null and store file list to output.txt, we type:
redirect error spam to /dev/null ##
find . -iname "data*.txt" -print 2>/dev/null > output.txt
How to Exclude all "permission denied" messages from the "find" command on Linux ?
The one problem with the following command is that it would filter out all error messages created by the find command and not just the permission denied ones:
find / -name foo 2>/dev/null
find / -type d -name bar 2>/dev/null
In order to avoid that, we 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"
Also, we can use the below 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, we 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
Then we display output.txt using the cat command:
In the above example, we used the find command along with grep command to filter out permission denied error messages.