清理linux 某個文件夾下面所有的log文件


#!/bin/sh
#目標文件夾下面所有問題
target_dir="/app/"
#刪除2天前新建的后綴為log的文件
find /app/applog*/ -ctime +2 -name "*.log" -exec rm -rf {} \;
#查找出文件夾下所有的log文件
list=`find $target_dir  -name "*.log"`
for file in $list 
do
    if test -f $file
        then 
        #清理   
        cat /dev/null > $file
        ls -l $file
    fi
done
df

 第二種,會有顯示,好用點

#!/bin/sh
#配置目標文件夾
target_dir="/app/"
#find $target_dir -ctime +2 -name "*.log" -exec ls -l {} \;
#直接刪除
#find $target_dir -ctime +2 -name "*.log" -exec rm -rf {} \;
# 先在目標目錄下查找出最后修改時間為2天前的,並且后綴為.log的文件  再循環刪除日志文件
list=`find $target_dir -mtime +2 -name "*.log"`
for dele_file in $list
do
    if test -f $dele_file
        then
        pwd_info=`ls -l $dele_file`
        rm -rf $dele_file
       echo "刪除文件:"$pwd_info
    fi
done
#先查找出 目標目錄下所有的后綴為.log 的文件,再循環清空日志
list=`find $target_dir  -name "*.log"`
for file in $list 
do
    if test -f $file
       then        
       cat /dev/null > $file
       pwd_info=`ls -l $file`
       echo "清空文件:"$pwd_info
    fi
done

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM