【转】收藏:shell目录管理
1 pasttime=`date --date='1 month ago' +%Y%m%d` 2 stat /data/tmp/*|sed -n '/File/p;/Modify/p'|awk '{print $2}'|sed "s/'//g;s/\`//g" |awk '{if(NR%2 !=0){en=$0} else {sub(/-/,"");sub(/-/,"");print en,$0}}'|awk "{if(\$2 <= $pasttime) {print \$1}}" >/tmp/delfile 3 while read line 4 do 5 echo "start clean~~~~~~~~~~~~~~~~" 6 rm -f /data/tmp/$line 7 done </tmp/delfile
[root@support-20 ~]# stat test
File: `test'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: fd00h/64768d Inode: 3373430 Links: 9
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2012-09-10 15:20:13.000000000 +0800
Modify: 2012-09-10 15:04:28.000000000 +0800
Change: 2012-09-10 15:04:28.000000000 +0800
|sed -n '/File/p;/Modify/p'
过滤出名字和修改时间行
File: `test'
Modify: 2012-09-10 15:04:28.000000000 +0800
awk '{print $2}'|sed "s/'//g;s/\`//g"
过滤第二列,去掉文件名的单引号
test
2012-09-10
awk '{if(NR%2 !=0){en=$0} else {sub(/-/,"");sub(/-/,"");print en,$0}}'
合并行
生成文件名 时间
test 20120910
awk "{if(\$2 <= $pasttime) {print \$1}}"
比较时间是否属于删除文件
其中$pasttime前面定义了
原文:http://bbs.chinaunix.net/thread-3770210-1-1.html