免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2713 | 回复: 9
打印 上一主题 下一主题

删除比指定文件早N天的文件的办法 又麻烦大家了!!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-06-23 19:04 |只看该作者 |倒序浏览
本帖最后由 x3 于 2010-06-23 19:10 编辑

在AIX 5.3 的kshell下

一个目录中 又很多 HXXXXX.log的文件 XXX为数字 按+1增加

现在想删除 指定某个文件如 H46546465.log(此文件不固定) 比他还陈旧3天的时间(mtime)的日志文件

find ./ -name "*.log" ! -newer H46546465.log我现在可以删除比这个文件早的  

但是不能实现 删除比指定文件早N天的文件的 办法

各位高手 给个友情赞助下  谢谢了

论坛徽章:
0
2 [报告]
发表于 2010-06-23 19:10 |只看该作者
你加上mtime那个参数阿

论坛徽章:
0
3 [报告]
发表于 2010-06-25 19:09 |只看该作者
老兄 不行吧 mtime 是以当前时间为参照物的

我的要求是以 某个文件的mtime为参照物的

给位 给个建议吧

论坛徽章:
0
4 [报告]
发表于 2010-06-25 20:49 |只看该作者
  1. suntao@~/abc$ ls -lt
  2. 总计 0
  3. -rw-r--r-- 1 suntao suntao 0 2010-06-23 00:00 101.txt
  4. -rw-r--r-- 1 suntao suntao 0 2010-06-22 00:00 102.txt
  5. -rw-r--r-- 1 suntao suntao 0 2010-06-20 00:00 103.txt
  6. -rw-r--r-- 1 suntao suntao 0 2010-06-20 00:00 104.txt
  7. -rw-r--r-- 1 suntao suntao 0 2010-06-20 00:00 105.txt
  8. -rw-r--r-- 1 suntao suntao 0 2010-06-18 00:00 106.txt
  9. -rw-r--r-- 1 suntao suntao 0 2010-06-18 00:00 107.txt
  10. -rw-r--r-- 1 suntao suntao 0 2010-06-15 00:00 108.txt
  11. suntao@~/abc$ ls -lt|awk -v D=4 '$NF=="102.txt"{day=substr($6,9,2);next}{if(substr($6,9,2)==day-D)print $NF}'
  12. 106.txt
  13. 107.txt
复制代码

论坛徽章:
0
5 [报告]
发表于 2010-06-25 22:46 |只看该作者

  1. #!/usr/bin/perl
  2. use File::stat;
  3. use File::Find;
  4. $source_time=stat ($ARGV[0])->mtime;
  5. $real_time=$source_time-86400*3;
  6. find (\&rules , ".");
  7. sub rules{
  8.         $file_time=stat($File::Find::name)->mtime;
  9.         $file_time<$real_time
  10.         &&
  11.         (print "delete $File::Find::name\n")
  12.         &&
  13.         (unlink $File::Find::name);

  14. }

复制代码
  1. 22:43:51 >ls -l
  2. total 4
  3. -rwxr-xr-x 1 moerae moerae 339 Jun 25 22:40 del.pl
  4. -rw-r--r-- 1 moerae moerae   0 Jun 24 00:00 file1
  5. -rw-r--r-- 1 moerae moerae   0 Jun 15 00:00 file10
  6. -rw-r--r-- 1 moerae moerae   0 Jun 23 00:00 file2
  7. -rw-r--r-- 1 moerae moerae   0 Jun 22 00:00 file3
  8. -rw-r--r-- 1 moerae moerae   0 Jun 21 00:00 file4
  9. -rw-r--r-- 1 moerae moerae   0 Jun 20 00:00 file5
  10. -rw-r--r-- 1 moerae moerae   0 Jun 19 00:00 file6
  11. -rw-r--r-- 1 moerae moerae   0 Jun 18 00:00 file7
  12. -rw-r--r-- 1 moerae moerae   0 Jun 17 00:00 file8
  13. -rw-r--r-- 1 moerae moerae   0 Jun 16 00:00 file9
  14. 22:43:53 >./del.pl file3
  15. delete ./file7
  16. delete ./file10
  17. delete ./file9
  18. delete ./file8
  19. 22:44:03 >ls -l
  20. total 4
  21. -rwxr-xr-x 1 moerae moerae 339 Jun 25 22:40 del.pl
  22. -rw-r--r-- 1 moerae moerae   0 Jun 24 00:00 file1
  23. -rw-r--r-- 1 moerae moerae   0 Jun 23 00:00 file2
  24. -rw-r--r-- 1 moerae moerae   0 Jun 22 00:00 file3
  25. -rw-r--r-- 1 moerae moerae   0 Jun 21 00:00 file4
  26. -rw-r--r-- 1 moerae moerae   0 Jun 20 00:00 file5
  27. -rw-r--r-- 1 moerae moerae   0 Jun 19 00:00 file6
  28. 22:44:05 >
复制代码

论坛徽章:
0
6 [报告]
发表于 2010-07-08 14:57 |只看该作者
谢谢 楼上 我试试看

论坛徽章:
0
7 [报告]
发表于 2010-07-09 18:49 |只看该作者
Usage: ./this.sh URFILE NUM

  1. #!/bin/bash

  2. rt=$(date -d "1970-01-01 +$(echo "$(stat -c %Y $1)-$2*86400+28800" | bc) sec" +%F" "%T)
  3. touch -d "$rt" .temp
  4. find -type f -name "*.log" ! -newer .temp | xargs -n 20 rm -f
  5. rm -f .temp
复制代码

论坛徽章:
0
8 [报告]
发表于 2010-07-09 19:38 |只看该作者
贡献我的

older_than_days =+1
find $file -mtime $older_than_days    -exec rm -rf () \;

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
9 [报告]
发表于 2010-07-09 20:27 |只看该作者
回复 8# liyihongcug


你这个跟楼主想要的不是一回事

论坛徽章:
0
10 [报告]
发表于 2010-07-10 12:06 |只看该作者
回复 7# kwokcn


    学会了 使用 touch 时加 -d   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP