shell脚本删除文件夹早于n天 [英] Shell script delete folders older than n days

查看:210
本文介绍了shell脚本删除文件夹早于n天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有名字的目录,

2012-12-12
2012-10-12
2012-08-08

我将如何删除早于10天,一个bash shell脚本旧条目

How would i delete old entries that are older than 10 days with a bash shell script

推荐答案

这会做递归你:

find /path/to/base/dir/* -type d -ctime +10 -exec rm -rf {} \;

说明:


  • 找到:查找文件/目录/链接等UNIX命令

  • /路径/要/基/ DIR :目录启动你的搜索

  • 型ð:只找目录

  • -ctime +10 :只考虑只是那些修改时间早于10天

  • -exec ... \\; :找到的每个这样的结果,做下面的命令 ...

  • RM -rf {} :递归强制删除目录;在 {} 部分是哪里查找结果被从previous部分代入。

  • find: the unix command for finding files / directories / links etc.
  • /path/to/base/dir: the directory to start your search in.
  • -type d: only find directories
  • -ctime +10: only consider the ones with modification time older than 10 days
  • -exec ... \;: for each such result found, do the following command in ...
  • rm -rf {}: recursively force remove the directory; the {} part is where the find result gets substituted into from the previous part.

另外,使用:

find /path/to/base/dir/* -type d -ctime +10 | xargs rm -rf

这是一个比较有效,因为它相当于:

Which is a bit more efficient, because it amounts to:

rm -rf dir1 dir2 dir3 ...

,而不是

rm -rf dir1; rm -rf dir2; rm -rf dir3; ...

作为 -exec 方法。

注意:同时看到下面的@关于$ P $与现代版的pferred使用发现MarkReed的评论

Note: Also see @MarkReed's comment below regarding preferred usage with modern version of find.

这篇关于shell脚本删除文件夹早于n天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆