2

When running the find command, it may output "No such file or directory" errors.

As answered to the find - suppress "No such file or directory" errors question here: https://stackoverflow.com/a/45575053/7939871, redirecting file descriptor 2 to /dev/null will happily silences error messages from find, such as No such file or directory:

find yada-yada... 2>/dev/null

This is perfectly fine, as long as not using -exec to execute a command. Because 2>/dev/null will also silence errors from the executed command.

As an example:

$ find /root -exec sh -c 'echo "Error" >&2' {} \; 2>/dev/null
$ find /root -exec sh -c 'echo "Error" >&2' {} \;
Error
find: ‘/root’: Permission denied

Is there a way to silence errors from find while preserving errors from the executed command?

1
  • 1
    you might have to use find -print0 2> /dev/null | xargs -0 instead of find -exec Commented Apr 29, 2022 at 6:54

2 Answers 2

1

Using -exec option in find command is integration of xargs command into find command.

You can alwayes separate find from -exec by piping find output into xargs command.

For example:

 find / -type f -name "*.yaml" -print0 2>/dev/null | xargs  ls -l
 
Sign up to request clarification or add additional context in comments.

1 Comment

maybe xargs -0, but this is still different from find -exec and even more from find -execdir as the command will be executed even when nothing has been found.
1

If for some reason you can't use:

find . -print0 2>/dev/null | xargs -0 ...

Then here's a POSIX way to do the same thing:

find . -exec sh -c '"$0" "$@" 2>&3' ... {} + 3>&2 2>/dev/null

note: 3>&2 is located before 2>/dev/null

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.