223

Possible Duplicate:
How do I remove “permission denied” printout statements from the find program?

When I run this command in Linux (SuSE):

find / -name ant

I get many error messages of the form:

find: `/etc/cups/ssl': Permission denied

Does find take an argument to skip showing these errors and only try files that I have permission to access?

user710818
  • 2,469
  • 3
  • 18
  • 12
  • 7
    Since you ask about an argument to find, please consider: find / ! -readable -prune. This is like "prune those results not (!) readable". See: http://stackoverflow.com/questions/762348/how-can-i-exclude-all-permission-denied-messages-from-find – appas Sep 16 '16 at 11:21
  • Yeah! That's a proper way of doing it. And the reason I was searching, Redirecting STDERR should be something nobody should be allowed to ask :P – runlevel0 Jun 22 '22 at 12:04

1 Answers1

279

you can filter out messages to stderr. I prefer to redirect them to stdout like this.

 find / -name art  2>&1 | grep -v "Permission denied"

Explanation:

In short, all regular output goes to standard output (stdout). All error messages to standard error (stderr).

grep usually finds/prints the specified string, the -v inverts this, so it finds/prints every string that doesn't contain "Permission denied". All of your output from the find command, including error messages usually sent to stderr (file descriptor 2) go now to stdout(file descriptor 1) and then get filtered by the grep command.

This assumes you are using the bash/sh shell.

Under tcsh/csh you would use

 find / -name art |& grep ....
mivk
  • 3,596
Levon
  • 11,384
  • 4
  • 45
  • 41
  • 149
    or even just find / -name art 2>/dev/null – rush Jul 11 '12 at 20:06
  • 34
    @rush That would filter out all error messages, not just the permission denied ones – Michael Mrozek Jul 11 '12 at 23:08
  • 6
    Think about it, that's about all find CAN return as an error message. Disk full, disk read error, ... possible, but rare, and find isn't geared to handle those. You've got OTHER issues if those errors show up anyways. Find's own error result is even vague.. 0=good, !0=something happened. Not even really differentiating anything but pass/fail. – lornix Jul 13 '12 at 03:24
  • 2
    I'm with @MichaelMrozek on this, I would rather see all possible error messages and then decide to filter them out instead of sending them to the bit-bucket unseen. – Levon Jul 13 '12 at 03:35
  • 11
    When I use find 2>/dev/null I always want only the result set not any other error messages. When a result set returned with null I just run the "regular" find and figure out what the problem is. – Bálint Babics Jun 28 '18 at 14:34
  • 1
    @Levon, @Michael.. Thank for your answer. The "permission denied" scenario typically happens when you are searching over a lot of directories, such as / where you maynot know or don't want to explicitly chose the directories over whom you have read permissions. If you use pipe operators like in the example.. does find and grep run parallelly, or serially? If its running serially, wont the total execution time be significantly worse (maybe ~50% worse)? – alpha_989 Aug 05 '18 at 16:08
  • 1
    @alpha_989 in case you didn't already know UNIX-like systems (including Linux and OS X) run the whole pipeline in paraller using multiple CPU cores if available. There's some buffering on the pipeline by default (depending on programs somewhere between 4 KB and 4 MB) but buffering can be disabled if needed (man stdbuf) and then every byte gets delivered immediately. – Mikko Rantalainen Mar 12 '19 at 10:56
  • Can anyone plz explain what the " 2>&1" part is in this answer? Thanks! – user3768495 Jun 21 '19 at 18:27
  • 1
    @rush your solution is safer IMO as it better serves the use-case of re-using the output of find as input for another process: redirecting the stderr to stdout would, despite the grep -v, potentially feed what is at best ireelevant, at worst harmfull data to the next process in the chain. – Attila Nov 22 '19 at 15:42
  • 1
    2>/dev/null filter all errors so I can run -exec ls command too. find / [filters] 2>/dev/null -exec ls -la {} \; – Hassan Faghihi Feb 17 '21 at 07:46
  • 1
    @rush 's response is what 90% of people reading this question will want, but of course the technically more correct answer is accepted. Great for scripts or aliases but do you think anyone is going to want to type that command every time they run a quick find on /? – ghostly_s Feb 05 '23 at 02:24
  • The answer @rush gave is best. The find command should have an option to suppress error messages. It's just not user friendly. – R OMS Feb 19 '24 at 15:21