批量清理某目录下的文件或移除某目录下的文件

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/bash 
#author: QingFeng
#qq: 530035210
#blog: http://my.oschina.net/pwd/blog 
#批量清理某目录下的文件或移除某目录下的文件
#缺省的配置如下
#basedir=/data/db/renewal/snapshots   #执行目录
#mvdir=/data/move/$(date "+%Y%m%d")
#clear_before_days=90            #清理的时间,120代表120天前的数据  
#file_key="snapshot"             #清理文件包含关键字
logdir=/data/log/clear           #日志路径
log=$logdir/clear.log            #日志文件 
is_font=1                #终端是否打印日志: 1打印 0不打印 
is_log=0                 #是否记录日志: 1记录 0不记录
 
datef(){
date "+%Y-%m-%d %H:%M:%S"
}
print_log(){
if [[ $is_log -eq 1  ]];then
[[ -d $logdir ]] || mkdir -p $logdir
echo "[ $(datef) ] $1" >> $log
fi
if [[ $is_font -eq 1  ]];then
echo "[ $(datef) ] $1"
fi
}
check_dir(){
if [[ ! -d $basedir  ]];then
print_log "[ $(datef) ] 需要清理的目录不存在: $basedir"
exit
fi
}
 
static_sizes(){
size=$(du -sh $basedir |awk '{print $1}')
print_log  "现在该目录[$basedir]的大小为: $size"
static_files(){
if [[ $file_key = "all"  ]];then
file_num=$(find  $basedir   -mtime +$clear_before_days  -type f |wc -l)
if [[ $file_num -eq 0  ]];then
print_log  "$basedir目录下:现在$clear_before_days天以前的文件:总共文件为0个,退出清理/移除动作"
exit
else
print_log  "$basedir目录下:现在$clear_before_days天以前的文件有:$file_num个 "
fi
else
file_num=$(find  $basedir   -mtime +$clear_before_days  -type f  -name "$file_key*"  |wc -l)
if [[ $file_num -eq 0  ]];then
print_log  "$basedir目录下:现在含有关键字:$file_key以及$clear_before_days天以前的文件为0个,退出清理/移除动作"
exit
else
print_log  "$basedir目录下:现在含有关键字:$file_key以及$clear_before_days天以前的文件有:$file_num个 "
fi
fi
}
 
rm_files(){
if [[ $file_key == "all"  ]];then
find  $basedir   -mtime +$clear_before_days  -type f   -exec rm {} \;
if [[ $? -eq 0  ]];then
print_log  "$basedir目录下:$clear_before_days天以前的文件删除成功."
else
print_log  "$basedir目录下:$clear_before_days天以前的文件删除失败."
fi
else
find  $basedir   -mtime +$clear_before_days  -type f  -name "$file_key*" -exec rm {} \;
if [[ $? -eq 0  ]];then
print_log  "$basedir目录下:现在含有关键字:$file_key以及$clear_before_days天以前的文件删除成功."
else 
print_log  "$basedir目录下:现在含有关键字:$file_key以及$clear_before_days天以前的文件删除失败."
fi 
fi
}
mv_files(){
[[ -d $mvdir ]] || mkdir -p $mvdir
if [[ $file_key == "all"  ]];then
find  $basedir   -mtime +$clear_before_days  -type f   -exec mv {} $mvdir \;
if [[ $? -eq 0  ]];then
print_log  "$basedir目录下:$clear_before_days天以前的文件move成功."
else
print_log  "$basedir目录下:$clear_before_days天以前的文件move失败."
fi
else
find  $basedir   -mtime +$clear_before_days  -type f  -name "$file_key*" -exec mv {} $mvdir \;
if [[ $? -eq 0  ]];then
print_log  "$basedir目录下:目前含有关键字:$file_key以及$clear_before_days天以前的文件move成功."
else
print_log  "$basedir目录下:目前含有关键字:$file_key以及$clear_before_days天以前的文件move失败."
fi
fi
#static_sizes
#statics_total
#static_files 
#rm_files
#mv_files
#statics_total
if [[  "$1" != ""  ]];then
firt_args=$1
else
echo -e "
 批量清理某目录下的文件或移除某目录下的文件
 用法示例"
echo -e  "
clean.class.sh:
    ./clean.class.sh  delete  要删除文件的所在目录  要删除文件的关键词 要删除多少天以前的文件
    ./clean.class.sh  move    要移除文件的所在目录  要移除文件的关键词 要移除多少天以前的文件  移除的目标目录
exp:
    ./clean.class.sh  delete  /data/db/renewal/snapshots/   snapshot  120 
    ./clean.class.sh  move   /data/db/renewal/snapshots/    snapshot  120  /data/move
tips:
    直接删除/移动所有文件示例如下:
    ./clean.class.sh  delete  /data/db/renewal/snapshots/   all  120
    ./clean.class.sh  move   /data/db/renewal/snapshots/    all  120  /data/move
"
exit
fi 
if [[ $firt_args != "delete"    ]];then
if [[ $firt_args != "move"   ]];then
print_log  "第一个参数,只能是move或delete."
exit
fi
fi
if [[ $2 != ""  ]];then
basedir=$2
check_dir
if [[ $2 = "/"  ]];then
print_log  "第二个参数[执行目录],不能选择根目录"
exit
fi 
else
print_log  "第二个参数[执行目录],不能为空"
exit
fi 
if [[ $3 != ""  ]];then
file_key=$3
else
print_log  "第三个参数[关键词],不能为空"
exit
fi
if [[ $4 != ""  ]];then
clear_before_days=$4
else
print_log  "第四个参数[天数],不能为空"
exit
fi
if [[ $firt_args = "move"    ]];then
if [[ $5 != ""  ]];then
mvdir=$5
if [[ ! -d  $mvdir   ]];then
echo "[ $(datef) ] 要移除文件的目的目录不存在: $basedir"
exit
fi 
else
print_log  "第五个参数[目的目录],不能为空"
exit
fi
static_sizes
static_files
mv_files
static_sizes
fi
if [[ $firt_args = "delete"    ]];then
static_sizes
static_files 
rm_files
static_sizes
fi

执行结果:

坚持原创技术分享,您的支持将鼓励我继续创作!