Linux下安全删除大文件

2024-03-27  本文已影响0人  WinsonWong

Linux下安全删除大文件

文件过大, 直接进行删除会占用IO, 导致IO和CPU爆满, 可以使用以下方式进行删除.

$ vim bash.sh
LAGRE_FILE=$1
if [[ ! -f ${LAGRE_FILE} ]];then
        echo "${LAGRE_FILE} NOT FOUND, PLEASE CHECK FILE."
        exit 1
fi
#Get File Size
FILE_SIZE=`du -shm ${LAGRE_FILE} | awk -F ' ' '{print $1}'`
#Default Speed (MB) 
DELETE_SPEED=200
# Safe Rm Size (MB)
SAFE_SIZE=200
echo "CURRENT ${LAGRE_FILE} IS ${FILE_SIZE}M, NOW START DELETE..."
for ((i=${FILE_SIZE}; i>${SAFE_SIZE}; i=i-${DELETE_SPEED}))
do
        echo "truncate ${i}M ......";
        truncate -s ${i}M ${LAGRE_FILE} ;
        sleep 1;
done

LAST_FILE_SIZE=`du -shm ${LAGRE_FILE} | awk -F ' ' '{print $1}'`
echo "CURRENT ${LAGRE_FILE} IS ONLY ${LAST_FILE_SIZE}M, DELETE IT..."
rm -rf ${LAGRE_FILE}
if [[ ! -f ${LAGRE_FILE} ]];then
        echo "${LAGRE_FILE} DELTE SUCC."
        exit 2
else
        echo "${LAGRE_FILE} DELTE FAILED, PLEASE CHECK AND DELTE IT MANUAL"
        exit 2
fi
$ chmod u+x bash.sh
$ [sh] bash.sh /var/www/project/job/large-file.log
上一篇 下一篇

猜你喜欢

热点阅读