批量創(chuàng)建文件腳本
腳本功能:在/bash/test/log下生成2010年1至12月份名為 Trans_Debug_日期.log文件,每次執(zhí)行只能生成一個(gè)月份的log文件.不能一次生成多個(gè)月份的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 "正在創(chuàng)建${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 "正在創(chuàng)建${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 "正在創(chuàng)建${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 "正在創(chuàng)建${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 "正在創(chuàng)建${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
批量刪除文件腳本
腳本功能:該腳本主要以月份為參數(shù)來(lái)刪除創(chuàng)建文件腳本下生成的log文件,一次可刪除多個(gè)月份的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
測(cè)試創(chuàng)建文件腳本
[root@localhost test]# ./mk_file.sh 3
正在創(chuàng)建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
測(cè)試刪除文件腳本
[root@localhost test]# ./rm_log.sh 3
==========file delete==============
ls: /bash/test/log/Trans_Debug_201001*: 沒(méi)有那個(gè)文件或目錄
ls: /bash/test/log/Trans_Debug_201002*: 沒(méi)有那個(gè)文件或目錄
-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]#