混合一个文件夹下的文件

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#!/bin/bash
#Author:        Castle Liu
#Date:          2014/06/04
#Desc:          merge files from a given dir,files should start with ${_BASE_NAME}
#Usage:         mergeFile -D[-d] [-c] -[-m]


_YESTERDAY=`date -d yesterday +"%Y%m%d"`
_WORK_D=""
_DEFAULT_DIR="${_WORK_D}${_YESTERDAY}"
_BACKUP_DIR="${_WORK_D}"`date -d  yesterday +"%Y%m"`
_BASE_NAME=""
_FULL_NAME="${_BASE_NAME}.${_YESTERDAY}"
_HEADER='date +"%F %T"'
_LOG_TMP=''

###func###
#print
#echo strings in a more confertable way
print(){
        string=$1
        echo "`eval ${_HEADER}` ${string} "
}

#help
#print help info
help() {
        cat <<EOF
        -d  mandetory,conflict with [-D] option,specify the dir that hold the files to be merged.
        -D  mandetory,conflict with [-d] option,use the default dir,
        -c  optional,compress the file mergeed.
        -m  optional,move the file mergeed to backup dir,and the dir given(or default dir) will be delete.
        -h  print this info.
EOF
}

#error_handler
#exit function for the script
error_handler(){
        error_code=$1
        error_detail=$2
        level=$3
        print "error_code: ${error_code}"
        print "error_detail: ${error_detail}"
        if [[ ${level} -gt 3 ]];then
                pmail "${error_detail}" "${_LOG_TMP}"
                print "Sended excution log to Admin."
        fi

        if [[  ${level} -eq 2 ]];then
                print "Serious error,excution will be abord."
                exit ${quit_or_not}
        fi
} 

#targz
#use tar -czvf to compress a file
targz(){
        file=$1
        dir=`pwd`
        cd `dirname ${file}`
        print "Compressing file:${file}"
        tar -czvf `basename ${file}`.tar.gz `basename ${file}` > /dev/null
        rm -rf `basename ${file}`
        print 'done.'
        cd ${dir}
}

#merge_file
#pass in a dirname
merge_file(){
        dir=$1
        _pwd=`pwd`
        cd ${dir}
        print "Creating file:${_FULL_NAME}"
        touch ${_FULL_NAME}
        count=`ls ${_BASE_NAME}*.txt|wc -l`
        if [[ ${count} -gt 0 ]];then
            ls  ${_BASE_NAME}*.txt|while read file
                do
                if [[ "${file}" != "EOF" ]];then
                        print "Hanlering file:${file}"
                        cat ${file} >> ${_FULL_NAME}
                        rm -rf ${file}
                fi
                done
            print "File created."

            if [[ ${_COMPRESS} ]];then
                    targz "${_FULL_NAME}"
            fi

            if [[ ${_MOVE_TO_BACKUP_DIR} ]];then
                    mv "${_FULL_NAME}.tar.gz" ${_BACKUP_DIR}
                    if [[ $? -eq 0 ]];then
                        export _DELETE_DIR=true
                    fi 
            fi 
        else
                print "There is no required file in dir:${_DIR}"
                error_handler "002" "no_required_file" 2
        fi

        cd ${_pwd}
}

###Main###
#parsing args
while getopts "d:cmD" arg
do
        case ${arg} in
                d)
                        export _DIR="${OPTARG}"
                        export _USE_DIR=true
                        ;;
                c)
                        export _COMPRESS=true
                        ;;
                m)
                        export _MOVE_TO_BACKUP_DIR=true
                        ;;
                D)      export _USE_DEFAULT=true
                        ;;
                h)      help
                        exit 0
                        ;;
                ?)
                        error_handler 001 "param_error" 2
                        ;;
                esac
done

print "MergeFile start..."


if [[ ${_USE_DIR} ]];then
        print "Using dir:${_DIR}"
	if [[ -d ${_DIR} ]];then
                name=`basename ${_DIR}`
                export _FULL_NAME=${_BASE_NAME}.${name}
                export _BACKUP_DIR=${_WORK_D}${name:0:6}
                #Create backup dir if not exists.
                if [[ ! -d ${_BACKUP_DIR} ]];then
                        mkdir -p ${_BACKUP_DIR}
                fi
                merge_file ${_DIR}
                if [[ ${_DELETE_DIR} ]];then
                    print "Deleting dir:${_DIR}"
                    rm -rf ${_DIR}
                fi

        else
                print "DIR not exists,pls check."
                error_handler "003" "dir_not_exists." 2
        fi
elif [[ ${_USE_DEFAULT} ]];then
        print "USing Default dir:${_DEFAULT_DIR}"

        #Create backup dir if not exists.
        if [[ ! -d ${_BACKUP_DIR} ]];then
                mkdir -p ${_BACKUP_DIR}
        fi
        if [[ -d ${_DEFAULT_DIR} ]];then
                merge_file ${_DEFAULT_DIR}
                if [[ ${_DELETE_DIR} ]];then
                    print "Deleting dir:${_DEFAULT_DIR}"
                    rm -rf ${_DEFAULT_DIR}
                fi
        else
                print "Default dir not exists,pls check."
                error_handler "004" "default_dir_not_exists" 3
        fi
else
        error_handler 005 "param_error" 2
fi