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
find - suppress "No such file or directory" errors
提问by JacobIRR
I can use -s
in grep
to suppress errors, but I don't see an equivalent for the find
command in the man page... Is the only option to redirect STDERR>/dev/null?
我可以使用-s
ingrep
来抑制错误,但我find
在手册页中没有看到该命令的等效项...是重定向 STDERR>/dev/null 的唯一选项吗?
Or is there an option that handles this? (open to fancy awk
and perl
solutions if needed)
或者有没有处理这个的选项?(如果需要,可以接受幻想awk
和perl
解决方案)
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 stderr
with 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 find
will only search in directories that match this pattern.
如果至少有一个匹配的目录,那么您甚至不需要抑制stderr
,因为find
只会在匹配此模式的目录中进行搜索。