#!/bin/sh
#遍历当前目录,将日志压缩文件按照域名、日志日期分类
#为了日志统一管理,完善日志系统分析、采集、备份、统计等需求
#date:2015-10-29
#add by chen-123
for file in $(find . -name "*gz" -type f);
do
path=${file#*/}
cur_parent_dir=${path%%/*}
old_dir=${path%/*}
old_sub_dir=${old_dir%/*}
project_log_dir_name=$(echo ${file##*/}|awk -F '[-_]' '{print $1}')
log_filename=$(echo ${file##*/})
if [ "${cur_parent_dir}" != "${project_log_dir_name}" ];then
echo $log_filename
if [ "${old_dir}" = "${old_sub_dir}" ];then
test -d "./"$project_log_dir_name || mkdir -p "./"$project_log_dir_name
moveto_path="./"$project_log_dir_name"/"$log_filename
test -f "${moveto_path}" && moveto_path="./"$project_log_dir_name"/"${log_filename/[_-]/_${cur_parent_dir}_/}
test -f "$moveto_path" || mv $file $moveto_path
test -f "$moveto_path" && echo "move $file to $moveto_path success"
else
new_sub_dir=${old_dir##*/}
moveto_path=$project_log_dir_name"/"$new_sub_dir"/"$log_filename
test -d $project_log_dir_name"/"$new_sub_dir || mkdir -p $project_log_dir_name"/"$new_sub_dir
test -f "$moveto_path" && moveto_path="./"$project_log_dir_name"/"${log_filename/[_-]/_${cur_parent_dir}_/}
test -f "$moveto_path" || mv $file $moveto_path
test -f "$moveto_path" && echo "move $file to $moveto_path success"
fi
fi
done