-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogrm
executable file
·80 lines (71 loc) · 1.73 KB
/
logrm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
#write by shuaili
def_days=20
while getopts ":c:d:s:h" optname
do
case "$optname" in
"c")
count=$OPTARG
#echo "Option $optname has value $OPTARG"
;;
"d")
days=$OPTARG
#echo "Option $optname has value $OPTARG"
;;
"s")
size=$OPTARG
#echo "Option $optname has value $OPTARG"
;;
"h")
echo "help:logrm -c count -d days -s disk_size dir"
;;
"?")
echo "Unknown option $OPTARG"
;;
":")
echo "No argument value for option $OPTARG"
;;
*)
echo "Unknown error while processing options"
;;
esac
done
shift $(($OPTIND - 1))
dir=$1
mount_root_dir=$dir
df -h |grep -q "${mount_root_dir}$"
until [ $? -eq 0 ]
do
mount_root_dir=`dirname $mount_root_dir`
df -h |grep -q "${mount_root_dir}$"
done
if [ $dir ]
then
:
else
echo "help:logrm -c count -d days -s disk_size dir"
exit
fi
if [ $count ]
then
#echo "cd $dir;let tmp=`ls |wc -l`-$count;ls -t |tail -n $tmp|xargs rm -f"
cd $dir;let tmp=`ls |wc -l`-$count;ls -t |tail -n $tmp|xargs rm -f
fi
if [ $days ]
then
#echo "find $dir -mtime +$days |xargs rm -rf"
find $dir -type f -mtime +$days |xargs rm -rf
def_days=$days
fi
if [ $size ]
then
current_size=`df -h |grep "${mount_root_dir}$" |awk '{print $5}' |sed -e "s/%//"`
while [[ $current_size -gt $size ]] && [[ $def_days -gt 2 ]]
do
#echo "find $dir -mtime +$def_days |xargs rm -rf"
find $dir -type f -mtime +$def_days |xargs rm -rf
let def_days=$def_days-1
current_size=`df -h |grep "${mount_root_dir}$" |awk '{print $5}' |sed -e "s/%//"`
#echo $def_days $current_size
done
fi