2

I've got a setup where I need to use 'find' to find 0 or more files in a certain directory, however, 'find' always produces an error when there are no files that match a certain check. Is it possible to run 'find' while having it omit errors when no files match the pattern?

| |
  • 2
    GNU find does not report such errors by default, at least not on my systems. – user1686 Aug 21 '11 at 10:36
1

Like @grawity said find isn't expected to report these errors. Anyway if you want to suppress error output redirect stderr to /dev/null:

find  . HERE_YOUR_FIND_PARAMS 2&> /dev/null

One typical case in which find write to stderr happens when there is a directory in your find searching sub-path that hasn't the permission to be opened.

| |
  • 1
    Why do use 2&> instead of 2>? – Frank Meulenaar Dec 16 '14 at 9:06
  • Not really a full solution because the exit code will still be 1 instead of zero, likely to break scripts. – sorin Oct 11 '17 at 12:46
1

Can you just ignore the errors? Or, more specifically, redirect them to /dev/null?

| |
  • I'm using the results in a script, so no, I can't just ignore the errors. Redirecting stderr to /dev/null works, however. – Eli Aug 21 '11 at 19:39
0

The only working solution I found so far was:

find something 2&> /dev/null || true

This assures that final exit code is zero even when there are no matches and also hides the stdout errors.

| |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.