我想使用“find -exec”从层次结构中删除某些目录
find $rootdir -type d -name target -exec rm -rf {} \;
我收到错误消息
find: `foo/bar/target': No such file or directory
因为“find -exec”显然是按前序工作的,即它先完成工作,然后访问子进程。我不想简单地将错误消息重定向到 /dev/null,因为它们可能很有意义。有什么建议吗?
答案1
您正在寻找-d
或-depth
选项
-depth Always true; same as the -d option.
-d Cause find to perform a depth-first traversal, i.e., directories
are visited in post-order and all entries in a directory will be
acted on before the directory itself. By default, find visits
directories in pre-order, i.e., before their contents. Note, the
default is not a breadth-first traversal.
要小心-depth
,如果添加数字,其含义就会改变。