• 如来神掌第二式第二招----Shell应用案例之大文件删除


    ###############################################################################
    # Name : Mahavairocana                                                                                                                                           
    # Author : Mahavairocana                                                                                                                                         
    # QQ : 10353512                                                                                                                                                    
    # WeChat : shenlan-qianlan                                                                                                                                      
    # Blog : http://www.cnblogs.com/Mahavairocana/                                                                                                       
    # Description : You are welcome to reprint, or hyperlinks to indicate the                                                                        
    #                    source of the article, as well as author information.                                                                                ###############################################################################

    #!/bin/sh
    
    #**********************************************************************************
    # *  this tool is used for file size more than 1G, to make delete file smoothly
    # *  truncate chunk size is 1G
    # * 
    # *  USAGE: ./rmbigfile.sh file_name sleep_seconds
    # * 
    #***********************************************************************************
    
    
    if [ $# != 2 ] ; then
         echo "" ;
         echo "/**********************************************************************************";
         echo " *  this tool is used for file size more than 1G, to make delete file smoothly";
         echo " *  truncate chunk size is 1G";
         echo "/**********************************************************************************";
         echo "" ;
         echo "USAGE: $0 file_name sleep_seconds" ;
         echo "" ;
         exit;
    fi
    
    FILE_NAME=$1;
    SLEEP_SECONDS=$2;
    
    date "+%Y-%m-%d %H:%M:%S";
    echo "start...";
    
    
    FILE_SIZE=`ls -s $FILE_NAME | awk '{print $1}'`;
    FILE_SIZE_G=`expr $FILE_SIZE / 1000000`;
    
    if [ $FILE_SIZE_G == 0 ] ; then
         echo "$FILE_NAME is less than 1G";
         FILE_SIZE_G=1;
    else
         echo "$FILE_NAME is $FILE_SIZE_G G";
    fi
    
    echo "prepare to truncate file: $FILE_NAME (${FILE_SIZE_G}G)";
    echo "truncate chunk size is 1G, looping every $SLEEP_SECONDS second";
    
    for i in `seq $FILE_SIZE_G -1 1 ` ;
         do sleep $SLEEP_SECONDS;
         cmd="truncate -s ${i}G $FILE_NAME";
         echo "*** $cmd";
         `$cmd`;
    done
    
    
    FILE_SIZE=`ls -s $FILE_NAME | awk '{print $1}'`;
    FILE_SIZE_G=`expr $FILE_SIZE / 1000000`;
    
    if [ $FILE_SIZE_G -le 1 ] ; then
            rm $FILE_NAME;
    fi
    
    echo "complete."
    date "+%Y-%m-%d %H:%M:%S"
  • 相关阅读:
    一文解读AI芯片之间的战争 (转)
    一文解读ARM架构 (转)
    一文解读云计算 (转)
    一文解读裸金属云 (转)
    一文解读发布策略 (转)
    C#使用OracleDataReader返回DataTable
    centos8平台上php7.4的生产环境配置
    centos8安装php7.4
    centos8安装java jdk 13
    docker的常用操作之二:docker内无法解析dns之firewalld设置等
  • 原文地址:https://www.cnblogs.com/Mahavairocana/p/8244202.html
Copyright © 2020-2023  润新知