分享

批量创建和删除文件脚本

 离魂异客 2010-12-29
批量创建文件脚本
脚本功能:在/bash/test/log下生成2010年1至12月份名为 Trans_Debug_日期.log文件,每次执行只能生成一个月份的log文件.不能一次生成多个月份的log文件
#/bin/bash
a=/bash/test/log
     if [ -n "$1" ];then
           if [ $1 -eq 1 ] || [ $1 -eq 3 ] || [ $1 -eq 5 ] || [ $1 -eq 7 ] || [ $1 -eq 8 ];then
                           echo "正在创建${1}月份日志文件....!";sleep 2
                       for (( i = 0; i <= 9; i++ ))
                           do
                              touch ${a}/Trans_Debug_20100${1}0$i.log
                              touch ${a}/Trans_Debug_20100${1}1$i.log
                              touch ${a}/Trans_Debug_20100${1}2$i.log
                       for (( x = 0; x <= 1; x++ ))
                           do
                              touch ${a}/Trans_Debug_20100${1}3$x.log
                              rm -rf ${a}/Trans_Debug_20100${1}00.log
                         done
                         done
                              ls -l ${a}/Trans_Debug_20100${1}* | awk '{ print $9}'
                       fi

     if [ $1 -eq 2 ]; then
             echo "正在创建${1}月份日志文件....!";sleep 2
          for (( i = 0; i <= 9; i++ ))
              do
                 touch ${a}/Trans_Debug_20100${1}0$i.log
                 touch ${a}/Trans_Debug_20100${1}1$i.log
                 touch ${a}/Trans_Debug_20100${1}2$i.log
                 rm -rf ${a}/Trans_Debug_20100${1}00.log
                 rm -rf ${a}/Trans_Debug_20100${1}29.log
            done
                 ls -l ${a}/Trans_Debug_20100${1}* | awk '{ print $9}'
         fi

     if [ $1 -eq 4 ] || [ $1 -eq 6 ] || [ $1 -eq 9 ]; then
              echo "正在创建${1}月份日志文件....!";sleep 2
          for (( i = 0; i <= 9; i++ ))
              do
                 touch ${a}/Trans_Debug_20100${1}0$i.log
                 touch ${a}/Trans_Debug_20100${1}1$i.log
                 touch ${a}/Trans_Debug_20100${1}2$i.log
           for (( x = 0; x <= 0; x++ ))
               do
                 touch ${a}/Trans_Debug_20100${1}3$x.log
                 rm -rf ${a}/Trans_Debug_20100${1}00.log
             done
             done
                 ls -l ${a}/Trans_Debug_20100${1}* | awk '{ print $9}'
              fi

      if [ $1 -eq 11 ]; then
                echo "正在创建${1}月份日志文件....!";sleep 2
              for (( i = 0; i <= 9; i++ ))
                   do
                      touch ${a}/Trans_Debug_2010${1}0$i.log
                      touch ${a}/Trans_Debug_2010${1}1$i.log
                      touch ${a}/Trans_Debug_2010${1}2$i.log
                for (( x = 0; x <= 0; x++ ))
                    do
                       touch ${a}/Trans_Debug_2010${1}3$x.log
                       rm -rf ${a}/Trans_Debug_2010${1}00.log
                   done
                   done
                       ls -l ${a}/Trans_Debug_2010${1}* | awk '{ print $9}'
                  fi                    
     if [ $1 -eq 10 ] || [ $1 -eq 12 ]; then
              echo "正在创建${1}月份日志文件....!";sleep 2
            for (( i = 0; i <= 9; i++ ))
                do
                   touch ${a}/Trans_Debug_2010${1}0$i.log
                   touch ${a}/Trans_Debug_2010${1}1$i.log
                   touch ${a}/Trans_Debug_2010${1}2$i.log
             for (( x = 0; x <= 1; x++ ))
                 do
                    touch ${a}/Trans_Debug_2010${1}3$x.log
                    rm -rf ${a}/Trans_Debug_2010${1}00.log
               done
               done
                    ls -l ${a}/Trans_Debug_2010${1}* | awk '{ print $9}'
               fi
         else
                   echo "invalid command!"
               fi

批量删除文件脚本
脚本功能:该脚本主要以月份为参数来删除创建文件脚本下生成的log文件,一次可删除多个月份的log文件.
#/bin/bash
clear
log_file=/bash/test/log
  if [ -n "$1" ];then
       echo "==========file delete=============="
  if [ $1 -le 9 ];then
          for (( i = 1; i <= $1; i++ ))
               do
                 ls -l $log_file/Trans_Debug_20100${i}*
                 rm -f $log_file/Trans_Debug_20100${i}*
            done
         fi
  if [ $1 -gt 9 ];then
          for (( i = 1; i <= 9; i++ ))
               do
                 ls -l $log_file/Trans_Debug_20100${i}*
                 rm -f $log_file/Trans_Debug_20100${i}*               
           for (( x = 10; x <= $1; x++ ))
               do
                  ls -l $log_file/Trans_Debug_2010${x}*
                  rm -f $log_file/Trans_Debug_2010${x}*
             done    
       done
             fi
     else
          echo "Command failed ERR!"
   fi
测试创建文件脚本
[root@localhost test]# ./mk_file.sh 3
正在创建3月份日志文件....!
/bash/test/log/Trans_Debug_20100301.log
/bash/test/log/Trans_Debug_20100302.log
/bash/test/log/Trans_Debug_20100303.log
/bash/test/log/Trans_Debug_20100304.log
/bash/test/log/Trans_Debug_20100305.log
/bash/test/log/Trans_Debug_20100306.log
/bash/test/log/Trans_Debug_20100307.log
/bash/test/log/Trans_Debug_20100308.log
/bash/test/log/Trans_Debug_20100309.log
/bash/test/log/Trans_Debug_20100310.log
/bash/test/log/Trans_Debug_20100311.log
/bash/test/log/Trans_Debug_20100312.log
/bash/test/log/Trans_Debug_20100313.log
/bash/test/log/Trans_Debug_20100314.log
/bash/test/log/Trans_Debug_20100315.log
/bash/test/log/Trans_Debug_20100316.log
/bash/test/log/Trans_Debug_20100317.log
/bash/test/log/Trans_Debug_20100318.log
/bash/test/log/Trans_Debug_20100319.log
/bash/test/log/Trans_Debug_20100320.log
/bash/test/log/Trans_Debug_20100321.log
/bash/test/log/Trans_Debug_20100322.log
/bash/test/log/Trans_Debug_20100323.log
/bash/test/log/Trans_Debug_20100324.log
/bash/test/log/Trans_Debug_20100325.log
/bash/test/log/Trans_Debug_20100326.log
/bash/test/log/Trans_Debug_20100327.log
/bash/test/log/Trans_Debug_20100328.log
/bash/test/log/Trans_Debug_20100329.log
/bash/test/log/Trans_Debug_20100330.log
/bash/test/log/Trans_Debug_20100331.log
[root@localhost test]# ls log/
Trans_Debug_20100301.log  Trans_Debug_20100308.log  Trans_Debug_20100315.log  Trans_Debug_20100322.log  Trans_Debug_20100329.log
Trans_Debug_20100302.log  Trans_Debug_20100309.log  Trans_Debug_20100316.log  Trans_Debug_20100323.log  Trans_Debug_20100330.log
Trans_Debug_20100303.log  Trans_Debug_20100310.log  Trans_Debug_20100317.log  Trans_Debug_20100324.log  Trans_Debug_20100331.log
Trans_Debug_20100304.log  Trans_Debug_20100311.log  Trans_Debug_20100318.log  Trans_Debug_20100325.log
Trans_Debug_20100305.log  Trans_Debug_20100312.log  Trans_Debug_20100319.log  Trans_Debug_20100326.log
Trans_Debug_20100306.log  Trans_Debug_20100313.log  Trans_Debug_20100320.log  Trans_Debug_20100327.log
Trans_Debug_20100307.log  Trans_Debug_20100314.log  Trans_Debug_20100321.log  Trans_Debug_20100328.log
 
测试删除文件脚本
[root@localhost test]# ./rm_log.sh 3
==========file delete==============
ls: /bash/test/log/Trans_Debug_201001*: 没有那个文件或目录
ls: /bash/test/log/Trans_Debug_201002*: 没有那个文件或目录
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100301.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100302.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100303.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100304.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100305.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100306.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100307.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100308.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100309.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100310.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100311.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100312.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100313.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100314.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100315.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100316.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100317.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100318.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100319.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100320.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100321.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100322.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100323.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100324.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100325.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100326.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100327.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100328.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100329.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100330.log
-rw-r--r--  1 root root 0  5月 13 20:09 /bash/test/log/Trans_Debug_20100331.log
[root@localhost test]# ls log/
[root@localhost test]#

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多