bash 用于删除超过 n 天的目录的 Shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13868821/
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
Shell script to delete directories older than n days
提问by bobsr
I have directories named as:
我的目录命名为:
2012-12-12
2012-10-12
2012-08-08
How would I delete the directories that are older than 10 days with a bash shell script?
如何使用 bash shell 脚本删除超过 10 天的目录?
回答by sampson-chen
This will do it recursively for you:
这将为您递归执行:
find /path/to/base/dir/* -type d -ctime +10 -exec rm -rf {} \;
Explanation:
解释:
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
: 用于查找文件/目录/链接等的 unix 命令。/path/to/base/dir
: 开始搜索的目录。-type d
: 只查找目录-ctime +10
: 只考虑修改时间超过10天的-exec ... \;
:对于找到的每个这样的结果,在...
rm -rf {}
: 递归强制删除目录;该{}
部分是将查找结果替换为前一部分的位置。
Alternatively, use:
或者,使用:
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 ...
as opposed to:
与:
rm -rf dir1; rm -rf dir2; rm -rf dir3; ...
as in the -exec
method.
就像在-exec
方法中一样。
With modern versions of find
, you can replace the ;
with +
and it will do the equivalent of the xargs
call for you, passing as many files as will fit on each exec system call:
使用现代版本的find
,您可以替换;
with +
,它将xargs
为您执行等效的调用,传递适合每个 exec 系统调用的尽可能多的文件:
find . -type d -ctime +10 -exec rm -rf {} +
回答by pmgarvey
If you want to delete all subdirectories under /path/to/base
, for example
如果你想删除所有子目录下/path/to/base
,例如
/path/to/base/dir1
/path/to/base/dir2
/path/to/base/dir3
but you don't want to delete the root /path/to/base
, you have to add -mindepth 1
and -maxdepth 1
options, which will access only the subdirectories under /path/to/base
但你不想删除 root /path/to/base
,你必须添加-mindepth 1
和-maxdepth 1
选项,它只会访问下的子目录/path/to/base
-mindepth 1
excludes the root /path/to/base
from the matches.
-mindepth 1
/path/to/base
从匹配中排除根。
-maxdepth 1
will ONLYmatch subdirectories immediately under /path/to/base
such as /path/to/base/dir1
, /path/to/base/dir2
and /path/to/base/dir3
but it will not list subdirectories of these in a recursive manner. So these example subdirectories will not be listed:
-maxdepth 1
将只匹配立即下的子目录/path/to/base
,如/path/to/base/dir1
,/path/to/base/dir2
和/path/to/base/dir3
,但它不会在递归的方式对这些列表子目录。所以这些示例子目录不会被列出:
/path/to/base/dir1/dir1
/path/to/base/dir2/dir1
/path/to/base/dir3/dir1
and so forth.
等等。
So , to delete all the sub-directories under /path/to/base
which are older than 10 days;
所以,删除所有/path/to/base
超过10天的子目录;
find /path/to/base -mindepth 1 -maxdepth 1 -type d -ctime +10 | xargs rm -rf
回答by Ondra ?i?ka
find
supports -delete
operation, so:
find
支持-delete
操作,所以:
find /base/dir/* -ctime +10 -delete;
I think there's a catch that the files need to be 10+ days older too. Haven't tried, someone may confirm in comments.
我认为有一个问题,文件也需要提前 10 天以上。没试过,有人可以在评论中确认。
The most voted solution here is missing -maxdepth 0
so it will call rm -rf
for every subdirectory, after deleting it. That doesn't make sense, so I suggest:
此处缺少投票最多的解决方案,-maxdepth 0
因此它会rm -rf
在删除每个子目录后调用它。这没有意义,所以我建议:
find /base/dir/* -maxdepth 0 -type d -ctime +10 -exec rm -rf {} \;
The -delete
solution above doesn't use -maxdepth 0
because find
would complain the dir is not empty. Instead, it implies -depth
and deletes from the bottom up.
-delete
上面的解决方案不使用,-maxdepth 0
因为find
会抱怨目录不为空。相反,它-depth
从下往上暗示和删除。
回答by user_v
I was struggling to get this right using the scripts provided above and some other scripts especially when files and folder names had newline or spaces.
我正在努力使用上面提供的脚本和其他一些脚本来解决这个问题,尤其是当文件和文件夹名称有换行符或空格时。
Finally stumbled on tmpreaper and it has been worked pretty well for us so far.
最后偶然发现了 tmpreaper,到目前为止它对我们来说效果很好。
tmpreaper -t 5d ~/Downloads
tmpreaper --protect '*.c' -t 5h ~/my_prg
Original Source link
原始来源链接
Has features like test, which checks the directories recursively and lists them. Ability to delete symlinks, files or directories and also the protection mode for a certain pattern while deleting
具有像 test 这样的功能,它递归地检查目录并列出它们。能够删除符号链接、文件或目录以及删除时特定模式的保护模式
回答by Awesome
OR
或者
rm -rf `find /path/to/base/dir/* -type d -mtime +10`
Updated, faster version of it:
更新,更快的版本:
find /path/to/base/dir/* -mtime +10 -print0 | xargs -0 rm -f