【问题标题】:Delete folders older then 1 day not working with "find" cmd删除超过 1 天的文件夹无法使用“查找”命令
【发布时间】:2019-05-17 10:21:44
【问题描述】:

我正在尝试使用 find 命令删除早于 1 天(创建日期)的备份文件夹,但它不起作用

文件夹 ls -l:

drwxrws---+ 2 root data 42 Mai 15 16:46 15-05-2019
drwxrws---+ 2 root data 89 Mai 16 14:19 16-05-2019

创建日期为 5 月 15 日。

这个 cmd 应该可以工作:

find /data/backup/VMs/centos/ -type d -mtime +1 -exec rm {} \;

我先尝试了这个,看看在删除之前会发生什么:

find /data/backup/VMs/centos/ -type d -mtime +1 -exec ls {} \; >> find_test.txt

应该把要删除的文件夹写入文件,但是txt文件是空的。

除了使用查找之外,如何使用名称中的日期删除此文件夹?

【问题讨论】:

  • UNIX 不存储文件的创建日期,但看起来您的意思是修改日期。您没有在问题中显示运行命令时的日期/时间是什么 - 您是否从手册页 When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago. 中考虑到(同样适用于 mtime)?
  • 感谢您的帮助,如果我打开文件夹,日期会更改吗?
  • “文件夹”我相信是一个 Windows 术语,在 UNIX 中有目录和文件。当您打开(访问)某些内容时,您会更改它的访问时间,当您修改它时,您会更改它的修改时间 - 两者都可以通过 statfind 等提供。

标签: bash


【解决方案1】:

rm 通常不会在标准输出上打印,但是如果发生错误,它会打印到标准错误,该错误也可以重定向到另一个文件或复制到相同的文件描述符 2>&1

find /data/backup/VMs/centos/ -type d -mtime +1 -exec ls {} \; >> find_test.txt 2>&1

可以使用find -print动作来打印名称,find也有动作-delete-ls(与ls不完全相同)以避免对每个文件执行命令

find /data/backup/VMs/centos/ -type d -mtime +1 -print -delete >> find_test.txt 2>&1

在使用-delete 之前要小心,以免丢失不需要的文件

【讨论】:

  • 还有其他方法可以按名称中的日期删除文件夹吗?
猜你喜欢
  • 2015-10-02
  • 1970-01-01
  • 2018-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多