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 do 16 [ -d $dev"/somePath" ] && point=$dev && break 17 done 18 echo $point 19 } 20 21 # Start here... 22 point=$(grep ‘/path;‘ $NGX_CONF |awk -F / ‘{print "/"$2}‘) 23 if [ ! -z $point ] 24 then 25 res=$(echo $point |grep -v ‘ ‘ |wc -l) 26 [ $res -eq 0 ] && point=`get_point` 27 else 28 point=`get_point` 29 fi 30 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 do 37 [ $i == $point"/somePath" ] && continue 38 #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 $i 41 done
时间: 2024-10-13 11:30:34