> shell编程 >

shell命令按照时间删除文件

其实,就是find命令的应用,主要用到了参数-mtime而已。
供初学者参考。

复制代码代码如下:
[root@localhost audit]# ll 
总用量 14788 
-rw------- 1 root root 4632650 10月 19 09:12 audit.log 
-r-------- 1 root root 5243056 10月 16 18:32 audit.log.1 
-r-------- 1 root root 5242984 10月 13 19:33 audit.log.2 

[root@localhost audit]# find . -mtime +7 -exec rm -rf {} \; 

[root@localhost audit]# ll 
总用量 14788 
-rw------- 1 root root 4632650 10月 19 09:12 audit.log 
-r-------- 1 root root 5243056 10月 16 18:32 audit.log.1 
-r-------- 1 root root 5242984 10月 13 19:33 audit.log.2 

[root@localhost audit]# find . -mtime +4 -exec rm -rf {} \; 

[root@localhost audit]# ll 
总用量 9660 
-rw------- 1 root root 4632650 10月 19 09:12 audit.log 
-r-------- 1 root root 5243056 10月 16 18:32 audit.log.1 
[root@localhost audit]#
(责任编辑:IT)