This repository has been archived by the owner on Jan 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun-classify.sh
232 lines (210 loc) · 6.75 KB
/
run-classify.sh
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/bash
function show_help() {
echo "用法: run-classify.sh [OPTION]... "
echo "分类该目录下的待分类的文件。 "
echo " "
echo "选项: "
echo " -e 替换文件名中的特殊字符,将' '、'\`'、'@'、'\\'、'\$'、'%'、'#'、"
echo " '{'和'}'删除掉,将'('、')'、'['、']'、'+'、'&'、','替换为'_'。 "
echo " -p, --pic 分类图片类型文件,需指定参数'name'、'createdtime'、'screenshot'、"
echo " 'wechat'和'otherpic'其中的一个。 "
echo " -f, --file 分类其他类型文件,需指定参数'word'、'excel'、'pdf'、'music'、 "
echo " 'text'、'compressionfile'和'ppt'其中的一个。 "
echo " --regstr 指定正则表达式,需与 --pic createdtime 一起使用。 "
echo " --test 只打印输出日志,而不实际执行命令。对于需要先改名再分类的文件则该"
echo " 选项只debug改名部分。 "
echo " --test2 只打印输出日志,而不实际执行命令。对于需要先改名再分类的文件则该"
echo " 选项只debug分类移动部分。 "
echo " -h, --help 显示帮助信息 "
echo " "
}
function corrent_filename() {
l_path=$1
l_IFS_OLD=$IFS
IFS=$'\n';
for l_item in `find $l_path -maxdepth 1`
do
if [ -d $l_item ]; then
continue;
fi
l_filename=$l_item;
l_filename=${l_filename//\`/};
l_filename=${l_filename//@/};
l_filename=${l_filename//\$/};
l_filename=${l_filename//%/};
l_filename=${l_filename//#/};
l_filename=${l_filename//\{/};
l_filename=${l_filename//\}/};
l_filename=${l_filename//\'/};
l_filename=${l_filename// /_};
l_filename=${l_filename//\(/_};
l_filename=${l_filename//\)/_};
l_filename=${l_filename//\[/_};
l_filename=${l_filename//\]/_};
l_filename=${l_filename//\+/_};
l_filename=${l_filename//\&/_};
l_filename=${l_filename//,/_};
if [ ! "$l_item" = "$l_filename" ]; then
echo "$cmd_debug_test1 : filename $l_item change to $l_filename"
if [ "$cmd_debug_test1" = "normal" ]; then
mv $l_item $l_filename;
fi
else
echo "$cmd_debug_test1 : filename $l_item do not need change"
fi
done
IFS=$l_IFS_OLD
}
ARG_CMD=`getopt -o ep:f:h -l file:,pic:,regstr:,test,test2,help -n "run-classify.sh" -- "$@"`;
eval set -- "$ARG_CMD"
cmd_debug_test1="";
cmd_debug_test2="";
cmd_regstr="";
file_type="";
file_sub_type="";
while true
do
case "$1" in
-p | --pic)
file_type="pic";
if [ "$2" != "name" -a "$2" != "createdtime" -a \
"$2" != "screenshot" -a \
"$2" != "wechat" -a "$2" != "otherpic" ]; then
echo -e "\033[31mError\033[0m --pic argument, must be one of \"name\",\"createdtime\",\"screenshot\",\"wechat\" and \"otherpic\".";
exit 1;
fi
file_sub_type=$2;
shift 2;
;;
-f | --file)
file_type="file";
if [ "$2" != "word" -a "$2" != "excel" -a "$2" != "pdf" -a \
"$2" != "compressionfile" -a "$2" != "music" -a \
"$2" != "text" -a "$2" != "ppt" ]; then
echo -e "\033[31mError\033[0m --file argument, must be one of \"word\",\"excel\",\"pdf\",\"compressionfile\",\"music\",\"text\",\"ppt\".";
exit 1;
fi
file_sub_type=$2;
shift 2;
;;
--regstr)
cmd_regstr=$2;
shift 2;
;;
--test)
cmd_debug_test1="debug";
shift;
;;
--test2)
cmd_debug_test2="debug";
shift;
;;
-e)
file_type="e";
file_sub_type="e";
shift;
;;
-h | --help)
show_help;
exit;
;;
--)
shift;
break;
;;
*)
exit 1;
;;
esac
done
if [ "$file_type" = "" -o "$file_sub_type" = "" ]; then
echo -e "\033[31mError\033[0m,Need options!"
exit 1;
fi
if [ "$file_type" = "pic" -a "$file_sub_type" = "createdtime" -a "$cmd_regstr" = "" ]; then
echo -e "\033[31mError\033[0m, createdtime option need regular expression string!";
exit 1;
fi
if [ "$cmd_debug_test1" = "" ]; then
cmd_debug_test1="normal";
fi
if [ "$cmd_debug_test2" = "" ]; then
cmd_debug_test2="normal";
fi
filelist_path=`tempfile -p fio`
echo $file_type - $file_sub_type - $cmd_debug_test1 - $cmd_debug_test2 - $cmd_regstr;
echo $file_sub_type $cmd_debug_test1 $cmd_debug_test2 $cmd_regstr> ${filelist_path};
ls -l --time-style long-iso | sed -r "/^total [0-9]*$|\
^d[ 0-9a-zA-Z\:\-]*Archives$|\
^d[ 0-9a-zA-Z\:\-]*Images$|\
^d[ 0-9a-zA-Z\:\-]*Manual$|\
^d[ 0-9a-zA-Z\:\-]*Music$|\
^d[ 0-9a-zA-Z\:\-]*Offices$|\
^d[ 0-9a-zA-Z\:\-]*Texts$|\
^d[ 0-9a-zA-Z\:\-]*Unclassification$|\
^d[ 0-9a-zA-Z\:\-]*.git$|\
^d[ 0-9a-zA-Z\:\-]*.script$|\
run-classify.sh$/d" >> ${filelist_path};
total_files=`cat ${filelist_path} | wc -l`
case "$file_type" in
pic)
case $file_sub_type in
name)
awk -vtotal_files=${total_files} -f ./Images/Camera/classify-photo-by-name.awk ${filelist_path};
;;
createdtime)
awk -vtotal_files=${total_files} -f ./Images/Camera/classify-photo-by-createdtime.awk ${filelist_path};
;;
wechat)
awk -vtotal_files=${total_files} -f ./Images/WeiXin/classify-wechat-pic.awk ${filelist_path};
;;
screenshot)
awk -vtotal_files=${total_files} -f ./Images/Screenshots/classify-screenshot-pic.awk ${filelist_path};
;;
otherpic)
awk -vtotal_files=${total_files} -f ./Images/OtherPics/classify-otherpics-pic.awk ${filelist_path};
;;
*)
;;
esac
;;
file)
case $file_sub_type in
word)
awk -vtotal_files=${total_files} -f ./Offices/Word/classify-word.awk ${filelist_path};
;;
excel)
awk -vtotal_files=${total_files} -f ./Offices/Excel/classify-excel.awk ${filelist_path};
;;
ppt)
awk -vtotal_files=${total_files} -f ./Offices/PPT/classify-ppt.awk ${filelist_path};
;;
pdf)
awk -vtotal_files=${total_files} -f ./Offices/PDF/classify-pdf.awk ${filelist_path};
;;
music)
awk -vtotal_files=${total_files} -f ./Music/classify-music.awk ${filelist_path};
;;
compressionfile)
awk -vtotal_files=${total_files} -f ./Archives/classify-compressionfile.awk ${filelist_path};
;;
text)
awk -vtotal_files=${total_files} -f ./Texts/classify-text.awk ${filelist_path};
;;
*)
;;
esac
;;
e)
#rename 's/ |`|@|\$|%|#|{|}//g' *;
#rename 's/\(|\)|\[|\]|\+|\&|,/_/g' *;
#rename 's/'\''/_/g' *;
corrent_filename "." "$cmd_debug_test1"
;;
*)
;;
esac
if [ -f "${filelist_path}" ]; then
rm "${filelist_path}"
fi
exit 0;