【发布时间】: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