删除早于linux中特定日期的文件

VRK*_*VRK 24 linux find delete-file

我使用以下命令删除超过一年的文件.

  find /path/* -mtime +365 -exec rm -rf {} \;
Run Code Online (Sandbox Code Playgroud)

但是,现在我要删除修改时间早于2014年1月1日的所有文件

我怎么在linux中做到这一点.

小智 30

这对我有用:

find /path ! -newermt "YYYY-MM-DD HH:MM:SS" | xargs rm -rf
Run Code Online (Sandbox Code Playgroud)

  • 对于文件`find /path!-type f -newermt "YYYY-MM-DD HH:MM:SS" -delete`。它使您不必通过 xargs 传输所有内容,也不必处理带有空格或其他破坏性字符的文件名。 (4认同)
  • 更正了命令 `find /path -type f ! -newermt“YYYY-MM-DD HH:MM:SS”-删除`谢谢@Shardj (3认同)
  • 这非常好,我不会用temp时间戳文件污染文件系统! (2认同)
  • 就其价值而言,“-newermt”是一个非标准扩展,尽管在 Linux 系统上您通常会使用 GNU“find”。这不能移植到其他平台。 (2认同)
  • 小心 @jbo5112 的命令,因为这将删除不是文件的所有内容, ! 需要移动到 -type f 的另一边 (2认同)

小智 21

您可以将时间戳作为文件触摸,并将其用作参考点:

例如2014年1月1日:

touch -t 201401010000 /tmp/2014-Jan-01-0000

find /path -type f ! -newer /tmp/2014-Jan-01-0000 | xargs rm -rf 
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为我们正在使用find一个-newer开关.

来自man 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.
Run Code Online (Sandbox Code Playgroud)

  • 谢谢.!和-not"找不到一样好.-not -newer/tmp/2014-Jan-01-0000" (3认同)
  • 这会污染文件系统。-newer(a/b/c/m/t) 时间戳可以有效地做到这一点。请更新你的答案 (3认同)

yog*_*oga 14

另一个答案会污染文件系统,并且find它本身提供了“删除”选项。因此,我们不必将结果通过管道传输到 xargs 然后发出 rm。

这个答案更有效:

find /path -type f -not -newermt "YYYY-MM-DD HH:MI:SS" -delete
Run Code Online (Sandbox Code Playgroud)


bun*_*nty 7

find ~ -type f ! -atime 4|xargs ls -lrt
Run Code Online (Sandbox Code Playgroud)

这将列出4 天前访问过的文件,从主目录搜索。