【问题标题】:How to delete 1 day old files in linux如何在 linux 中删除 1 天前的文件
【发布时间】:2016-11-16 06:11:53
【问题描述】:

我看到很多在 Linux 中删除 n 天前的文件的例子,但没有一个对我有用。以下是我使用的一些命令

[root@centos dmp]# find . -mtime +1 -exec rm -Rf -- {} \;
[root@centos dmp]# ll
total 3259164
-rw-r----- 1 oracle dba   12754944 Jul 11 21:01 GSW-11-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 12 02:30 GSW-12-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 13 02:30 GSW-13-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 11 21:01 MI-11-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 12 02:31 MI-12-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 13 02:30 MI-13-07-2016.dmp
[root@centos dmp]# find . -name "*.dmp*" -ctime +1 -exec rm {} \;
[root@centos dmp]# ll
total 3259164
-rw-r----- 1 oracle dba   12754944 Jul 11 21:01 GSW-11-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 12 02:30 GSW-12-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 13 02:30 GSW-13-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 11 21:01 MI-11-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 12 02:31 MI-12-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 13 02:30 MI-13-07-2016.dmp
[root@centos dmp]# echo find . * -type f -mtime +1 -exec rm {} \;
find . GSW-11-07-2016.dmp GSW-12-07-2016.dmp GSW-13-07-2016.dmp MI-11-07-2016.dmp MI-12-07-2016.dmp MI-13-07-2016.dmp -type f -mtime +1 -exec rm {} ;
[root@centos dmp]# find . * -type f -mtime +1 -exec rm {} \;
[root@centos dmp]# ll
total 3259164
-rw-r----- 1 oracle dba   12754944 Jul 11 21:01 GSW-11-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 12 02:30 GSW-12-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 13 02:30 GSW-13-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 11 21:01 MI-11-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 12 02:31 MI-12-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 13 02:30 MI-13-07-2016.dmp
[root@centos dmp]# find . * -mtime +1 -exec rm {} \;
[root@centos dmp]# ll
total 3259164
-rw-r----- 1 oracle dba   12754944 Jul 11 21:01 GSW-11-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 12 02:30 GSW-12-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 13 02:30 GSW-13-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 11 21:01 MI-11-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 12 02:31 MI-12-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 13 02:30 MI-13-07-2016.dmp
[root@centos dmp]# find . * -type f -mtime +1 -exec rm {} \;
[root@centos dmp]# ll
total 3259164
-rw-r----- 1 oracle dba   12754944 Jul 11 21:01 GSW-11-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 12 02:30 GSW-12-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 13 02:30 GSW-13-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 11 21:01 MI-11-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 12 02:31 MI-12-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 13 02:30 MI-13-07-2016.dmp
[root@centos dmp]# find . * -mtime +1 -exec rm -Rf -- {} \;
[root@centos dmp]# ll
total 3259164
-rw-r----- 1 oracle dba   12754944 Jul 11 21:01 GSW-11-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 12 02:30 GSW-12-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 13 02:30 GSW-13-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 11 21:01 MI-11-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 12 02:31 MI-12-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 13 02:30 MI-13-07-2016.dmp
[root@centos dmp]# find . -type f -mtime +1 -name '*.dmp' -execdir rm -- {} \;
[root@centos dmp]# ll
total 3259164
-rw-r----- 1 oracle dba   12754944 Jul 11 21:01 GSW-11-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 12 02:30 GSW-12-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 13 02:30 GSW-13-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 11 21:01 MI-11-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 12 02:31 MI-12-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 13 02:30 MI-13-07-2016.dmp
[root@centos dmp]# find . -type f -ctime +1 -name '*.dmp' -execdir rm -- {} \;
[root@centos dmp]# ll
total 3259164
-rw-r----- 1 oracle dba   12754944 Jul 11 21:01 GSW-11-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 12 02:30 GSW-12-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 13 02:30 GSW-13-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 11 21:01 MI-11-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 12 02:31 MI-12-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 13 02:30 MI-13-07-2016.dmp
[root@centos dmp]# find . -type f -mtime +1 -name '*.dmp' -print0 | xargs -r0 rm --
[root@centos dmp]# ll
total 3259164
-rw-r----- 1 oracle dba   12754944 Jul 11 21:01 GSW-11-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 12 02:30 GSW-12-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 13 02:30 GSW-13-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 11 21:01 MI-11-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 12 02:31 MI-12-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 13 02:30 MI-13-07-2016.dmp

2) 我也在尝试从 ftp 远程服务器中删除旧文件并将我的问题发布在这里 https://stackoverflow.com/questions/38309645/linux-script-to-delete-ndays-old-ftp-files

更新:
我尝试了答案,但仍然没有运气

[root@centos dmp]# find . -type f -mtime +1 -exec rm -rf {} \;
[root@centos dmp]# ll
total 3259164
-rw-r----- 1 oracle dba   12754944 Jul 13 17:16 GSW-13-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 14 02:30 GSW-14-07-2016.dmp
-rw-r----- 1 oracle dba   12754944 Jul 15 02:30 GSW-15-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 13 17:16 MI-13-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 14 02:30 MI-14-07-2016.dmp
-rw-r----- 1 oracle dba 1099702272 Jul 15 02:30 MI-15-07-2016.dmp
[root@centos dmp]# 

请告知我该如何解决它。感谢期待

【问题讨论】:

  • 如果您指定ls 而不是rm,您会看到列出的文件吗?
  • 投票支持ls 想法而不是rm
  • 投票支持lsidea 而不是rm,很酷,那么你得到了什么结果?是否符合预期(您看到了要删除的文件的名称)?我刚试过find . -mtime +1 -exec rm -Rf -- {} \;,对我来说效果很好。
  • 我的错。我错误地使用了 rm -rf *.dmp 并删除了所有文件。现在至少要等一天再检查一下
  • 不要将-rf作为选项,它不是必需的,可以保存您的数据以防出错。

标签: linux bash shell unix centos


【解决方案1】:

试试:

find . -type f -mtime +1 -exec rm -rf {} \;

【讨论】:

  • 为什么-rf 用于删除文件?如果 OP 只想删除文件,我会添加 -type f 以确保它不会删除目录。
  • @Marki555 - 我添加了-type 建议,因为我认为这是个好主意。 OP 从未提及有关递归和强制删除的任何内容,但示例表明它正在被使用,尽管使用的是 -R。因此,我根据我从 OP 的示例中收集到的信息保留了 -rf。谢谢。
  • 但是问题标题确实暗示 OP 对文件感兴趣,而不是目录。
  • @EJK - 是的,确实如此。我刚刚添加了-type 选项以响应@Marki555 的cmets。谢谢。
  • @tale852150 我更新了我的问题并复制了您的命令的粘贴输出。你可以看到它不工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
  • 1970-01-01
  • 2014-07-15
  • 1970-01-01
相关资源
最近更新 更多