bash 查找 - 抑制“没有这样的文件或目录”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45575021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:21:23  来源:igfitidea点击:

find - suppress "No such file or directory" errors

bashunixfind

提问by JacobIRR

I can use -sin grepto suppress errors, but I don't see an equivalent for the findcommand in the man page... Is the only option to redirect STDERR>/dev/null?

我可以使用-singrep来抑制错误,但我find在手册页中没有看到该命令的等效项...是重定向 STDERR>/dev/null 的唯一选项吗?

Or is there an option that handles this? (open to fancy awkand perlsolutions if needed)

或者有没有处理这个的选项?(如果需要,可以接受幻想awkperl解决方案)

Example:

例子:

$ for dir in `ls /mnt/16_c/`; do find /mnt/16_c/$dir/data/ -mtime +180 -type f -exec echo {} \;; done
find: `/mnt/16_c/test_container/dat/': No such file or directory

回答by janos

You can redirect stderrwith 2>/dev/null, for example:

您可以重定向stderr使用2>/dev/null,例如:

find /mnt/16_c/$dir/data/ -mtime +180 -type f -exec echo {} \; 2>/dev/null

Btw, the code in your question can be replaced with:

顺便说一句,您问题中的代码可以替换为:

find /mnt/16_c/*/data/ -mtime +180 -type f 2>/dev/null

And if there is at least one matching directory, then you don't even need to suppress stderr, because findwill only search in directories that match this pattern.

如果至少有一个匹配的目录,那么您甚至不需要抑制stderr,因为find只会在匹配此模式的目录中进行搜索。