查找:删除目录后无法chdir到目录错误消息

时间:2012-09-12 13:27:25

标签: bash shell unix automation

我正在开发一个项目来自动删除超过一天的特定目录。我有以下代码工作:

find Directory/ -type d -name "Directory.To.Delete.*" -mtime +1 -exec rm -rf {} \;

运行正常并按预期删除目录(和内容),但它总是以错误结束:

find: cannot chdir to Directory/ : No such file or directory

有没有办法在不面对此错误的情况下运行此代码?我不明白为什么这段代码在删除目录后尝试使用chdir。

2 个答案:

答案 0 :(得分:6)

-prune之前添加-exec;这将阻止find输入匹配目录:

.... -mtime +1 -prune -exec ...

答案 1 :(得分:1)

在-type d之前添加-depth也会阻止查找输入匹配的目录。

# mkdir 1
# mkdir 2
# touch -d yesterday 1
# find . -depth -type d -mtime +0 -exec rm -rf {} \;
# ls
2

从男人那里找到:

   -prune True;  if  the file is a directory, do not descend into it. If -depth is given, false; no effect.  Because -delete implies -depth, you cannot usefully use -prune and -delete together.