在Linux中,如何删除早于特定日期的文件?
我使用下面的命令删除了一年以上的文件在Linux中,如何删除早于特定日期的文件?,linux,find,delete-file,Linux,Find,Delete File,我使用下面的命令删除了一年以上的文件 find /path/* -mtime +365 -exec rm -rf {} \; 但现在我想删除修改时间早于2014年1月1日的所有文件。在Linux中如何实现这一点?您可以将时间戳作为文件,并将其用作参考点: e、 g.2014年1月1日: touch -t 201401010000 /tmp/2014-Jan-01-0000 find /path -type f ! -newer /tmp/2014-Jan-01-0000 | xargs
find /path/* -mtime +365 -exec rm -rf {} \;
但现在我想删除修改时间早于2014年1月1日的所有文件。在Linux中如何实现这一点?您可以将时间戳作为文件,并将其用作参考点: e、 g.2014年1月1日:
touch -t 201401010000 /tmp/2014-Jan-01-0000
find /path -type f ! -newer /tmp/2014-Jan-01-0000 | xargs rm -rf
这是因为find
有一个我们正在使用的更新的开关
从人工查找:
-newer file
File was modified more recently than file. If file is a symbolic
link and the -H option or the -L option is in effect, the modification time of the
file it points to is always used.
这对我很有用:
find /path ! -newermt "YYYY-MM-DD HH:MM:SS" | xargs rm -rf
这将列出从主目录搜索时访问的超过4天的文件。污染了文件系统,find
本身提供了一个“删除”选项。因此,我们不必将结果通过管道传输到xargs,然后再发布rm
这个答案更有效:
find /path -type f -not -newermt "YYYY-MM-DD HH:MI:SS" -delete
非常感谢。与-而不是“查找-而不是-更新的/tmp/2014-Jan-01-0000”一样好这会污染文件系统-较新的(a/b/c/m/t)时间戳可以有效地实现这一点。请更新您的answer@yoga或者在完成后删除文件。它在“临时文件”目录中,这很好,我不会用临时时间戳文件污染文件系统!仅用于文件find/path-类型f-newermt“YYYY-MM-DD HH:MM:SS”-删除
。它使您不必通过XARG传输所有内容,也不必处理带有空格或其他破坏性字符的文件名。值得一提的是,-newermt
是一个非标准扩展,不过在Linux系统上通常会有GNUfind
。这不能移植到其他平台。数据fmt错误:使用find/path-type f-not-newermt“YYYY-MM-DD HH:MI:SS”-delete这似乎重复,尽管-delete
选项是一种改进。
find /path -type f -not -newermt "YYYY-MM-DD HH:MI:SS" -delete