有没有什么简单的方法可以在 bash 中使用“rm”来“按日期删除”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/434851/
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
Is there any trivial way to 'delete by date' using ′rm'- in bash?
提问by Fergie
I noticed today (after ~8 years of happily hacking away at bash) that there is no trivial way to 'delete by date' using ′rm'. The solution is therefore to pipe stuff around a combination of commands like rm, ls, find, awk and sed.
我今天注意到(在 bash 愉快地砍掉了大约 8 年之后),没有使用“rm”“按日期删除”的简单方法。因此,解决方案是围绕 rm、ls、find、awk 和 sed 等命令的组合进行管道传输。
Say for example I wanted to delete every file in the working directory from 2009, what would be the typical approach?
比如说我想从 2009 年开始删除工作目录中的每个文件,典型的方法是什么?
I came up with the following, which is butt-ugly and should only be run if 'rm' is set to skip over directories (otherwise you will delete the parent directory):
我想出了以下内容,这很丑陋,只有在 'rm' 设置为跳过目录时才应该运行(否则您将删除父目录):
ls -la | awk '{if (substr(,0,5)==2009) print }' | xargs rm
Points for both the most elegant and the most outrageously over-engineered solutions.
最优雅和最令人发指的过度设计的解决方案的积分。
回答by flolo
I would combine find and rm (without pipe)
我会结合 find 和 rm (没有管道)
find . ...bunch of find criterias to select certain files (e.g. by date) .... -exec rm \{\} \;
EDIT: with parameters for your example it would be
编辑:使用您的示例参数,它将是
find . -maxdepth 1 -type f -ctime -12 -exec rm \{\} \;
CAVEAT: This works just today :-). (To make it work everytime, replace the -ctime with absoulte time, see timeXY in the manpage )
警告:这在今天才有效:-)。(要使其每次都工作,请将 -ctime 替换为绝对时间,请参阅手册页中的 timeXY)
回答by Paul Dixon
Some versions of find support the -delete option, making it even more efficient...
某些版本的 find 支持 -delete 选项,使其更加高效...
find . -maxdepth 1 -type f -ctime -12 -delete;
Check your find man page (this has worked on most recent releases of Ubuntu for me)
检查您的查找手册页(这对我来说适用于最新版本的 Ubuntu)
回答by Alastair
I would use:
我会用:
find . -maxdepth 1 -type f -newerct 'jan 1' -print0 \
| xargs -0 rm
(or -newermtif you want to filter on modification time)
(或者-newermt如果你想过滤修改时间)
Note that the 't' form of -newerXY will allegedtly allow any date format compatible with cvs (see doco).
请注意,据称 -newerXY 的“t”形式将允许任何与 cvs 兼容的日期格式(请参阅doco)。
回答by Keltia
find(1)is a much more efficient to do what you want than parsing ls(1)output.
find(1)做你想做的事比解析ls(1)输出更有效。
EDIT: something to watch for is filenames with spaces in them so you want to have a findwhich supports -print0(to be used with xargs -0) for better performance.
编辑:需要注意的是其中包含空格的文件名,因此您希望有一个find支持-print0(与 一起使用xargs -0)以获得更好的性能。
find . -mtime +12 -print0 | xargs -0 rm -f
回答by ordnungswidrig
Instead of parsing ls(1) which can too easily break you should rely on stat(1):
而不是解析很容易破坏的 ls(1) 你应该依赖 stat(1):
stat -c '%z/%n' files_or_glob | grep '^date' | cut -d/ -f2- | xargs -d '\n' rm -i
e.g.
例如
$ stat -c '%z/%n' *| grep '^2008-12-16' | cut -d/ -f2- | xargs -d '\n' rm -i
Note: this will not handle filenames with embedded newlines correctly. However they are rarely found in the wil.d
注意:这不会正确处理带有嵌入换行符的文件名。然而,它们在野外很少被发现。

