运行find
命令时,可能会输出“No such file or directory”错误。
正如对 find - suppress "No such file or directory" errors 的回答问题在这里:https://stackoverflow.com/a/45575053/7939871 ,将文件描述符 2 重定向到 /dev/null
将会很高兴地消除来自 find
的错误消息,例如 No such file or directory
:
find yada-yada... 2>/dev/null
这完全没问题,只要不使用 -exec
来执行命令即可。因为 2>/dev/null
也会消除执行命令中的错误。
举个例子:
$ find /root -exec sh -c 'echo "Error" >&2' {} \; 2>/dev/null
$ find /root -exec sh -c 'echo "Error" >&2' {} \;
Error
find: ‘/root’: Permission denied
有没有办法消除 find
中的错误,同时保留执行命令中的错误?
最佳答案
在find
命令中使用-exec
选项是xargs
的集成命令转换为 find
命令。
您始终可以通过将 find
输出传输到 xargs
命令中来将 find
与 -exec
分开。
例如:
find / -type f -name "*.yaml" -print0 2>/dev/null | xargs ls -l
关于bash - find -exec - 仅抑制查找错误,但不抑制执行命令的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72031430/