使用 find 删除所有子目录(及其文件)
我确信这是直截了当的并且在某处得到了答案,但我没有找到我想要的东西。基本上,我试图每 7 天运行一个 cron 脚本来清除给定目录的内容。到目前为止,我已经尝试了以下操作,
find /myDir -mtime 7 -exec rm -rf {} \;
但这也会删除我不想要的父目录 myDir。我也尝试过,
find /myDir -type f -type d -mtime 7 -delete
但似乎没有任何作用。我也尝试过,
fnd /myDir -type d -delete
它根据我的需要删除了除父目录之外的所有目录。然而,出现了一条警告消息:
相对路径可能不安全
如果有人可以纠正我的脚本,以便它安全地删除文件夹中的所有子目录,我将不胜感激。
非常感谢。 =)
,我决定采取以下措施
find /myDir -mindepth 1 -mtime 7 -delete
更新:根据我从所有回复者那里了解到的情况 。再次非常感谢大家。
I'm sure this is straight forward and answered somewhere, but I didn't manage to find what I was looking for. Basically, I'm trying to run a cron script to clear the contents of a given directory every 7 days. So far I have tried the following,
find /myDir -mtime 7 -exec rm -rf {} \;
This however also deletes the parent directory myDir, which I do not want. I also tried,
find /myDir -type f -type d -mtime 7 -delete
which appeared to do nothing. I also tried,
fnd /myDir -type d -delete
which deleted all but the parent directory just as I need. However, a warning message came up reading,
relative path potentially not safe
I'd appreciate if anyone can rectify my script so that it safely deletes all subdirectories in folder.
Many thanks. =)
UPDATE: I decided to go for the following,
find /myDir -mindepth 1 -mtime 7 -delete
Based upon what I learned from all who replied. Again, many thanks to you all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试:
Try:
怎么样
假设您从
myDir
父目录运行它?如果你不能保证 myDir 存在,那么这更安全:
What about
assuming that you run this from
myDir
parent directory.If you can't guarantee myDir exists, then this is safer:
find /myDir -mindepth 1 -mtime 7 -delete
可能应该是
find /myDir -mindepth 1 -mtime +7 -delete
(或者可能是
mtime +6< /代码>)。
+
表示 7 天或更早的内容,而不是正好 7 天。find /myDir -mindepth 1 -mtime 7 -delete
should probably be
find /myDir -mindepth 1 -mtime +7 -delete
(or maybe
mtime +6
). The+
means things 7 days old or older rather than exactly 7 days.