首页 > 代码库 > 清理旧数据的小脚本

清理旧数据的小脚本

 

 1 #!/bin/bash 2 # Author      : standby 3 # Date        : 2017-05-09 4 # Description : Clean the old files and dirs to free the local disk. 5 #                   - file keep 2 days. 6 #                   - dir  keep 5 days and less 5 files contained. 7  8 NGX_CONF="/usr/local/nginx/conf/nginx.conf" 9 10 # Get the dev which contains ./path/ 11 function get_point()12 {13     arr=(`df -HT |grep -v Filesystem |grep -E [0-9]{1,2}T |awk {print $NF}`)14     for dev in ${arr[*]}15     do16         [ -d $dev"/somePath" ] && point=$dev && break17     done18     echo $point19 }20 21 # Start here...22 point=$(grep /path; $NGX_CONF |awk -F / {print "/"$2})23 if [ ! -z $point ]24 then25     res=$(echo $point |grep -v   |wc -l)26     [ $res -eq 0 ] && point=`get_point`27 else28     point=`get_point`29 fi30 31 # Clean the old files which contains [*.cpp | *.jpg | *.c] and keep 2 days only.32 find $point/somePath -ctime +1 -type f -name "*.c" -exec /bin/rm -f {} \;33 34 # Clean the old ./path/some_sub_dirs which created 5 days ago and has less 5 files contained.35 for i in $(find $point/somePath -ctime +4 -type d ! -name "*except_dir*")36 do37     [ $i == $point"/somePath" ] && continue38     #ls -ld --time-style=long-iso $i |awk {print $6" "$7}39     num=$(ls -l $i |grep ^- |wc -l)40     [ $num -lt 5 ] && rm -rf $i41 done

 

清理旧数据的小脚本