- How can I exclude all «permission denied» messages from «find»?
- 21 Answers 21
- Ignore permission denied message from find command in Linux
- Find command basic syntax
- How we tackle ‘ignore permission denied message from find’
- Conclusion
- PREVENT YOUR SERVER FROM CRASHING!
- Permission denied in find: why do we need 2>&1?
- 2 Answers 2
- Bash- find- permission denied stuck?
How can I exclude all «permission denied» messages from «find»?
I am experimenting when such message arises. I need to gather all folders and files, to which it does not arise. Is it possible to direct the permission levels to the files_and_folders file? How can I hide the errors at the same time?
Great question! Unfortunately, the first three answers simply do not work on Debian Linux. Or at least my configuration of it. I needed Fatih’s solution, find /. -name ‘toBeSearched.file’ 2>/dev/null .
I found it best to exclude the /proc path using the -path option. It helps to negate the -prune option to avoid printing pruned items.
21 Answers 21
find . 2>/dev/null > files_and_folders
This hides not just the Permission denied errors, of course, but all error messages.
If you really want to keep other possible errors, such as too many hops on a symlink, but not the permission denied ones, then you’d probably have to take a flying guess that you don’t have many files called ‘permission denied’ and try:
find . 2>&1 | grep -v 'Permission denied' > files_and_folders
If you strictly want to filter just standard error, you can use the more elaborate construction:
find . 2>&1 > files_and_folders | grep -v 'Permission denied' >&2
The I/O redirection on the find command is: 2>&1 > files_and_folders | . The pipe redirects standard output to the grep command and is applied first. The 2>&1 sends standard error to the same place as standard output (the pipe). The > files_and_folders sends standard output (but not standard error) to a file. The net result is that messages written to standard error are sent down the pipe and the regular output of find is written to the file. The grep filters the standard output (you can decide how selective you want it to be, and may have to change the spelling depending on locale and O/S) and the final >&2 means that the surviving error messages (written to standard output) go to standard error once more. The final redirection could be regarded as optional at the terminal, but would be a very good idea to use it in a script so that error messages appear on standard error.
There are endless variations on this theme, depending on what you want to do. This will work on any variant of Unix with any Bourne shell derivative (Bash, Korn, …) and any POSIX-compliant version of find .
If you wish to adapt to the specific version of find you have on your system, there may be alternative options available. GNU find in particular has a myriad options not available in other versions — see the currently accepted answer for one such set of options.
Ignore permission denied message from find command in Linux
While running find command, received an error ‘ignore permission denied message from find’? We can help you.
Here at Bobcares, we have seen several such Linux-related errors as part of our Server Management Services for web hosts, Linux users, and online service providers.
Today we’ll take a look at the cause for this error and see how to fix it.
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/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"
cd /tmp find . -iname "data*.txt" -print
How we tackle ‘ignore permission denied message from find’
Now let’s take a look at how our Support Engineers tackle the error when executing the find command.
Recently, one of our customers was trying to execute the below find command in Linux/Unix and received “Permission denied” error messages.
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 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:
./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
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 cat output.txt
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.
[Need any further assistance in Linux-related errors? – We’re available to help you]
Conclusion
In short, this error ‘ignore permission denied message from find’ occurs while running a ‘find’ command in Linux. Today, we saw how our Support Engineers resolve this error.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
Permission denied in find: why do we need 2>&1?
I was recently searching for a way to filter out ‘Permission denied‘ errors while searching for a file using the find command, and I found this link: How can I exclude all «permission denied» messages from «find»? Here’s an the answer that worked for me from the link:
find . -name "filename" 2>&1 | grep -v 'permission denied'
Although this answer works, I don’t fully understand why we need the 2>&1 (redirection of stderr to stdout). Doesn’t the output from the command find . -name «filename» | grep -v ‘Permission denied’ already redirect to stdout?
It is enormously important to understand that pruning the error messages will not make find succeed. In other words, even if you 2>/dev/null , your find will still fail (set a non-zero exit code). This is particularly painful when there is set -e in the script that uses find.
2 Answers 2
Because the ‘permission denied’ message is printed in stderr not stdout.
& specifies that whatever following is a file descriptor not filename
2>&1 redirects stderr to stdout and enables the error message to be piped into the grep command.
If excluding permission denied message is all you need, then this will do it without using grep :
find . -name "filename" 2>/dev/null
But if it’s printed to stderr, why do I see «find . ‘/path/to/file : Permission denied’ » each time? It seems like it’s printing to stdout in this case.
When using pipes you should keep in mind that only the standard output is sent to the input of the other command, that’s why you need to redirect the stderr to the stdout and then send the stdout (2>&1).
will show the lines that do NOT contain «permission denied».
this solution works but it’s far from optimal.
whenever you want to get rid of the error you shoud send it to /dev/null:
/dev/null is like a blackhole, anything you send there will just be lost.
another use of /dev/null is truncating files(for learning only, there is adequate functions to do that properly) :
the file will be empty after issuing this command.
you should also note that the first thing the bash do is interpreting redirections for example :
will create a file foobar
let’s say we want to send the stdout and the stderr of one command to a file:
in the first example, the bash will :
1/redirect the stderr to the stdout
2/ redirect the stdout to file
3/execute command
and that’s why :
won’t work as expected.
Sometimes you will want to send the stdout to output.txt and stderr to error.txt :
$ command > ouput.txt 2> error.txt
Bash- find- permission denied stuck?
I am trying to get files older than 3 days (by modified date, mtime ) by find command (i want to exec rm command in future, doesnt matter why :D). Problem is I am not root user and there are mix of the files, of course. It looks, like simple script as hell gonna to stuck when occurs first permission denied message.
#!/bin/sh find /tmp* -type f -mtime +3 -group jenkins -print # -exec rm <> \;
Output is just error message (owned by root user) find: ‘/tmp/systemd-private-dbe33161924b4616a037608d052f48d0-httpd.service-sFr4Wd’: Permission denied Notice- there are many files which should occur in find command:
ll /tmp | less . drwxr-xr-x 2 jenkins jenkins 4096 Dec 11 02:33 02a47e28-4254-45d4-b69d-ed33b9ef3bad drwxr-xr-x 2 jenkins jenkins 4096 Dec 6 15:10 03a1acc7-3040-430a-b5c3-9ce646407b93 drwxr-xr-x 2 jenkins jenkins 4096 Dec 5 12:27 062eb875-3216-4b2a-bae2-66106b66b0cd drwxr-xr-x 2 jenkins jenkins 4096 Dec 9 06:51 0a6f8afc-0976-441b-980b-d0502f3903b1 drwxr-xr-x 2 jenkins jenkins 4096 Dec 5 13:07 1512475627515-0 drwxr-xr-x 2 jenkins jenkins 4096 Dec 5 13:07 1512475627836-0 drwxr-xr-x 2 jenkins jenkins 4096 Dec 5 13:07 1512475629807-0 drwxr-xr-x 2 jenkins jenkins 4096 Dec 5 13:07 1512475629829-0 drwxr-xr-x 2 jenkins jenkins 4096 Dec 5 13:07 1512475629843-0 . -rw-rw-r-- 1 root root 301 Dec 5 14:37 informix_disc.log -rw-rw-r-- 1 root root 714 Dec 5 14:37 oracle_disc.log -rw-rw-r-- 1 root root 602 Dec 5 14:37 sybase_disc.log drwx------ 3 root root 4096 Nov 20 13:47 systemd-private-dbe33161924b4616a037608d052f48d0-httpd.service-sFr4Wd drwxr-xr-x 2 root root 4096 Oct 30 09:39 vmware-config0 drwxr-xr-x 2 root root 4096 Nov 17 08:51 vmware-config1 .
1. 2>/dev/null OR 2>&- (add on the end, error output to null redirect) 2. find -type d ! -readable -prune -o . (exclude not readable folders) 3. find /tmp* -type f -mtime +3 -group jenkins -print 2>&1 | grep -v "Permission denied" (should be same as above) 4. find -user jenkins, as well as -user $(whoami) 5. -perm -u+rwx
- am I doing something wrong, or what is the reason it gots stucked after first permission denied message?
- Is there any better simple way how to do what I want?
- IS any way how to ignore not permitted folders and make it working?