你能否写出一手顺畅的shell脚本,在于你平时能不能坚持练习。既然我给大家提供了这个机会让你去练习,希望你要珍惜!只不过拿出几分钟的时间来看一看,写一写而已,有那么难吗?
今日shell题目:假如我们需要每小时都去执行你写的脚本。在脚本中实现这样的功能,当时间是0点和12点时,需要将目录/data/log/下的文件全部清空,注意只能清空文件内容而不能删除文件。而其他时间只需要统计一下每个文件的大小,一个文件一行,输出到一个按日期和时间为名字的日志里。 考虑/data/log/目录下的二级、三级。。。子目录里面的文件。
今日shell题目:假如我们需要每小时都去执行你写的脚本。在脚本中实现这样的功能,当时间是0点和12点时,需要将目录/data/log/下的文件全部清空,注意只能清空文件内容而不能删除文件。而其他时间只需要统计一下每个文件的大小,一个文件一行,输出到一个按日期和时间为名字的日志里。 考虑/data/log/目录下的二级、三级。。。子目录里面的文件。
0
{:5_121:}
- [root@www ~]# cat 0918.sh
- #!/bin/bash
- # Desc: None
- # Writen by Jeffery.su
- # Date : Sep 18,2014
- # Version: V1
- LOG_PATH=/data/log
- LOG_FILE=/tmp/`date +%y%m%d%H%M`.log_size
- CUR_H=`date +%H`
-
- if [ $CUR_H -eq 0 -o $CUR_H -eq 12 ];then
- for log in `find $LOG_PATH -type f`
- do
- > $log
- done
- else
- echo -e "$(date) begin check file size \n" > $LOG_FILE
- for LOG in `find $LOG_PATH -type f`
- do
- CUR_TIME=`date +'%Y/%m/%d %H:%M:%S'`
- CUR_SIZE=`du -sh $LOG|awk '{print $2"\t"$1}'`
- echo -e "$CUR_TIME $CUR_SIZE\r" >> $LOG_FILE
- done
- echo -e "\n$(date) check file size finished" >> $LOG_FILE
- fi
- echo "* */1 * * * /bin/bash /usr/local/bin/0918.sh" >>/var/spool/cron/root
0
#!/bin/bash
time=`date +%F-%H.log`
d=`date +%H`
if [ "$d" -eq 00 ] || [ "$d" -eq 12 ];then
dir=/data/log
for i in `ls /data/log`
do
:> $dir/$i
done
else
for i in `ls /data/log`
do
if [ -f /data/log/"$i" ];then
du -sh /data/log/$i>>/tmp/$time
fi
done
fi
time=`date +%F-%H.log`
d=`date +%H`
if [ "$d" -eq 00 ] || [ "$d" -eq 12 ];then
dir=/data/log
for i in `ls /data/log`
do
:> $dir/$i
done
else
for i in `ls /data/log`
do
if [ -f /data/log/"$i" ];then
du -sh /data/log/$i>>/tmp/$time
fi
done
fi
0
本帖最后由 齐天大圣 于 2014-9-18 15:36 编辑
- #!/bin/bash
- #Check the size of file in /data/web
- #Writen by Leechao
- #现在的时间
- date1=`date +%y%m%d%H`
- #现在的小时
- date2=`date +%H`
- #一个小时前的时间
- date3=`date -d "-1 hour" +%y%m%d%H`
- #日志
- log=$date1.log
- #一个小时前日志
- log1=$date3.log
- #目录
- dir=/data/log
- if [ $date2 -eq 00 -o $date2 -eq 12 ];then
- for f in `awk '{print $2}' $log1`
- do
- echo "" >$f
- done
- else
- find $dir -type f |xargs du -sh >/tmp/$log
- fi
- 00 */1 * * * /bin/bash /root/check_file_size.sh
0
本帖最后由 Louis 于 2014-9-22 10:34 编辑
- #!/bin/bash
- ## This script is for operate files under /data/log.
- ## At 0-12 clock,make all files empty.
- ## ohter time,list all files size to a log named by like yyyymmddHH.log.
- ## Written by Louis at 2014/09/18 14:50
- while :; do
- time=`date +%H`
- dir=/data/log
- if [ $time -ge 0 ] && [ $time -le 12 ]; then
- for f1 in `find $dir -type f`; do
- > $f1
- done
- else
- for f2 in `find $dir -type f`; do
- echo `du -sh $f2` >> /tmp/`date +%Y%m%d%H`.log
- done
- fi
- sleep 3600
- done
- 前面理解错题目意思了,是0点和12点,并不是0点至12点。
- 2014/09/22 10:30 更正:
- 第10行判断条件:if [ $time -ge 0 ] && [ $time -le 12 ]
- 更改为:if [ $time -eq 0 ] || [ $time -eq 12 ]
- 第3行备注行:## At 0-12 clock,make all files empty.
- 更改为:## At 0 or 12 clock,make all files empty.
0
#!/bin/bash
path="/data/log"
time=`date +%F-%T`
time1=`date +%H`
while :
do
if [ $time1 -eq 0 -o $time1 -eq 12 ]
then
find $path -type f -exec cp /dev/null {} \;
else
find $path -type f -exec ls -l {} \; | awk '{print $5,$NF}'>${time}.txt
fi
sleep 3600
done
如果不加sleep 3600的话,加到 crontab里面
* */1 * * * sh /path/clean.sh
path="/data/log"
time=`date +%F-%T`
time1=`date +%H`
while :
do
if [ $time1 -eq 0 -o $time1 -eq 12 ]
then
find $path -type f -exec cp /dev/null {} \;
else
find $path -type f -exec ls -l {} \; | awk '{print $5,$NF}'>${time}.txt
fi
sleep 3600
done
如果不加sleep 3600的话,加到 crontab里面
* */1 * * * sh /path/clean.sh
0
本帖最后由 wyatt88 于 2014-9-18 16:16 编辑
#!/bin/bash
#ww
#2014-09-18
basedir=/data/log/
filename=`date +%F_%T`.log
hour=`date +%H`
files=`find $basedir -type f`
if [ $hour == "00" -o $hour == "12" ]
then
echo "clear files at "$filename >> $filename
for file in $files
do
echo $file >> $filename
>$file
done
echo >> $filename
else
date +%F_%T>>$filename
for file in $files
do
fands=`find $basedir -type f -ls|grep $file |awk '{print $7,$11}'`
echo $fands >> $filename
done
echo >> $filename
fi
思路:先把文件都找出来,然后就是需要符合时间逻辑,利用条件判断下。
0 */1 * * * /root/20140918.sh
#!/bin/bash
#ww
#2014-09-18
basedir=/data/log/
filename=`date +%F_%T`.log
hour=`date +%H`
files=`find $basedir -type f`
if [ $hour == "00" -o $hour == "12" ]
then
echo "clear files at "$filename >> $filename
for file in $files
do
echo $file >> $filename
>$file
done
echo >> $filename
else
date +%F_%T>>$filename
for file in $files
do
fands=`find $basedir -type f -ls|grep $file |awk '{print $7,$11}'`
echo $fands >> $filename
done
echo >> $filename
fi
思路:先把文件都找出来,然后就是需要符合时间逻辑,利用条件判断下。
0 */1 * * * /root/20140918.sh
0
本帖最后由 游夜 于 2014-9-18 17:25 编辑
加入任务计划中
crontab -e
0 1 * * * /bin/sh /usr/local/sbin/cleanlog.sh
- #!/bin/bash
- #shell name cleanlog.sh
- if [ ! -d ~/log ] #判断是否存在存储日志的路径,不存在则创建
- then
- mkdir -p ~/log
- fi
- file=~/log/`date +%Y%m%d%H%M%S`.txt #在~/log里创建以日期时间命名的文件用于保存文件大小信息
- if [ `date +%H` -eq 0 ]||[ `date +%H` -eq 12 ] #判断时间是否为0点或12点,由于是每隔1小时执行一次,此处不对分钟进行比较
- then
- for i in `/bin/find /data/log -type f`;do #查找文件
- echo "" >$i #清空内容
- done
- else
- for i in `/bin/find /data/log -type f`;do #查找文件
- /usr/bin/du -sh $i >> $file #将文件大小信息存入以日期时间命名的文件,>>会自动换行
- done
- fi
加入任务计划中
crontab -e
0 1 * * * /bin/sh /usr/local/sbin/cleanlog.sh
0
本帖最后由 陈沛 于 2014-9-21 22:20 编辑
#!/bin/bash
# 2014-09-19
# chenpei
# The file size
# 时间判断 0,12时清空 其余记录文件大小
timeJudge() {
hours=`date %H`
if [ hours -eq 0 ] || [ hours -eq 12 ]
then
echo > $1
echo "清空文件内部数据,保留文件"
else
du -sh $1 >> /tmp/fileSize.log
echo "log存储位置:/tmp/fileSize.log"
fi
}
# 遍历/data/log下普通文件
for i in `find /data/log -type f`
do
timeJudge $i
done
/*******************************************************************************************/
命令: crontab -e
0 * * * * /bin/bash /data/fileSize.sh
#!/bin/bash
# 2014-09-19
# chenpei
# The file size
# 时间判断 0,12时清空 其余记录文件大小
timeJudge() {
hours=`date %H`
if [ hours -eq 0 ] || [ hours -eq 12 ]
then
echo > $1
echo "清空文件内部数据,保留文件"
else
du -sh $1 >> /tmp/fileSize.log
echo "log存储位置:/tmp/fileSize.log"
fi
}
# 遍历/data/log下普通文件
for i in `find /data/log -type f`
do
timeJudge $i
done
/*******************************************************************************************/
命令: crontab -e
0 * * * * /bin/bash /data/fileSize.sh
0
本帖最后由 jaws1689 于 2014-9-25 10:20 编辑
#!/bin/bash
#清空文件内容但不删除
a=`find /root/experiment -type f | xargs ls`
for i in $a
do
echo "">$i
done
#统计每个文件大小
while :
do
b=`date -d "-5 min" +%Y%m%d%H%M`
find /root/experiment -type f | xargs ls lh |awk '{print $9,$5}'|awk -F/ '{print $NF}' >>$b.log
sleep 300
done
crontab -e 0 0 * * * sh log01.sh
crontab -e 0 12 * * * sh log01.sh
/etc/rc.d/crond restart
#!/bin/bash
#清空文件内容但不删除
a=`find /root/experiment -type f | xargs ls`
for i in $a
do
echo "">$i
done
#统计每个文件大小
while :
do
b=`date -d "-5 min" +%Y%m%d%H%M`
find /root/experiment -type f | xargs ls lh |awk '{print $9,$5}'|awk -F/ '{print $NF}' >>$b.log
sleep 300
done
crontab -e 0 0 * * * sh log01.sh
crontab -e 0 12 * * * sh log01.sh
/etc/rc.d/crond restart
0
#!/bin/bash
datename=`date +%y%m%d`.log
if [ date +%H eq 0 ]||[ date +%H = 12 ]
then
for i in find /data/log/ -type f ;
do
>$i;
done
fi
if [date +%H != 0 ]||[ date +%H != 12 ]
then
for i in find /data/log/ -type f ;
do
du $i>>$dateneme;
done
fi
datename=`date +%y%m%d`.log
if [ date +%H eq 0 ]||[ date +%H = 12 ]
then
for i in find /data/log/ -type f ;
do
>$i;
done
fi
if [date +%H != 0 ]||[ date +%H != 12 ]
then
for i in find /data/log/ -type f ;
do
du $i>>$dateneme;
done
fi
0
#/bin/bash
#
#
TIME=`date +%H`
FILENAME=`date +%F-%H-%M-%S`.log
if [ $TIME -eq "00" -o $TIME -eq "12" ]; then
find /data/log -mindepth 1 -type f -exec cp /dev/null {} \;
else
ll -h /data/log |awk '{print $5}' > $FILENAME
fi
* */1 * * *
#
#
TIME=`date +%H`
FILENAME=`date +%F-%H-%M-%S`.log
if [ $TIME -eq "00" -o $TIME -eq "12" ]; then
find /data/log -mindepth 1 -type f -exec cp /dev/null {} \;
else
ll -h /data/log |awk '{print $5}' > $FILENAME
fi
* */1 * * *
0
#!/bin/bash
d=`date +%H`
d1=`date +"%Y-%m-%d-%H:%M:%S"`
echo $d
if [ $d -eq 0 ] || [ $d -eq 12 ];then
for i in `[ $d -eq 0 ] || [ $d -eq 12 ]`;do
echo "" >`find /data/log -type f`
sleep 216000
done
else
touch $d1.log
echo "du -c /data/log" >$d1.log
fi
不知道写的行不行,等待讲解。
d=`date +%H`
d1=`date +"%Y-%m-%d-%H:%M:%S"`
echo $d
if [ $d -eq 0 ] || [ $d -eq 12 ];then
for i in `[ $d -eq 0 ] || [ $d -eq 12 ]`;do
echo "" >`find /data/log -type f`
sleep 216000
done
else
touch $d1.log
echo "du -c /data/log" >$d1.log
fi
不知道写的行不行,等待讲解。
0
#! /bin/bash
date1=`date +%H`
date2=`date +%Y%m%d%H`
file=`find /data/log -type f`
if [ $date1 -eq 00 ] || [ $date1 -eq 12 ];then
for i in $file;do
cat /dev/null > $i
done
else
ls -lh $file >> $date2
fi
date1=`date +%H`
date2=`date +%Y%m%d%H`
file=`find /data/log -type f`
if [ $date1 -eq 00 ] || [ $date1 -eq 12 ];then
for i in $file;do
cat /dev/null > $i
done
else
ls -lh $file >> $date2
fi
0
#!/bin/bash
#practice for shell homework 2014-09-18
#20140921 zhuzhu
tm=`date +%T`
if [ $tm == "00:00:00"] || [ $tm == "12:00:00"]; then
find /data/log -type f > /data/filelist
for f in `cat /data/filelist`
do
echo "" > $f
done
else
find /data/log -type f | xargs du -sh > /tmp/"`date +%F-%T`"
fi
每个小时的执行打算用crontab,目前还不会
#practice for shell homework 2014-09-18
#20140921 zhuzhu
tm=`date +%T`
if [ $tm == "00:00:00"] || [ $tm == "12:00:00"]; then
find /data/log -type f > /data/filelist
for f in `cat /data/filelist`
do
echo "" > $f
done
else
find /data/log -type f | xargs du -sh > /tmp/"`date +%F-%T`"
fi
每个小时的执行打算用crontab,目前还不会
0
fands=`find $basedir -type f -ls|grep $file |awk '{print $7,$11}'`。我觉得grep在这里没起到作用哈
wyatt88 发表于 2014-9-18 16:14
#!/bin/bash
#ww
#2014-09-18
fands=`find $basedir -type f -ls|grep $file |awk '{print $7,$11}'`。我觉得grep在这里没起到作用哈
0
珠珠的也是标准答案哈
butterfly梧桐雨 发表于 2014-9-21 22:51
#!/bin/bash
#practice for shell homework 2014-09-18
#20140921 zhuzhu
珠珠的也是标准答案哈
0
额 好像是 其实循环的意义也木有 不知道当时想啥呢。。。{:4_103:}谢啦
齐天大圣 发表于 2014-9-23 09:25
fands=`find $basedir -type f -ls|grep $file |awk '{print $7,$11}'`。我觉得grep在这里没起到作用哈
额 好像是 其实循环的意义也木有 不知道当时想啥呢。。。{:4_103:}谢啦
0
- #!/bin/bash
- function read_dir(){
- for file in `ls $1`; do
- if [ -d $1"/"$file ]; then
- read_dir $1"/"$file $2 $3
- else
- if [ $3 -eq 0 ] || [ $3 -eq 12 ]; then
- > $1"/"$file
- else
- file_size=`ls -lh $1"/"$file |awk '{print $5}'`
- echo $1"/"$file $file_size >> $2
- fi
- fi
- done
- }
- dir="/data/log"
- log_file=`date +"%F_%T"`".log"
- hour=`date +%H`
- read_dir $dir $log_file $hour
0
#!/bin/bash
#
for i in `find /var/log -type f` ; do
date=`date +%H`
if [ $date -eq 0 ] || [ $date -eq 12 ] ; then
>$i
else
du -h $i >> /root/`date +%F-%T`.log
fi
done
0 * * * * /bin/bash /data/log_time.sh
#
for i in `find /var/log -type f` ; do
date=`date +%H`
if [ $date -eq 0 ] || [ $date -eq 12 ] ; then
>$i
else
du -h $i >> /root/`date +%F-%T`.log
fi
done
0 * * * * /bin/bash /data/log_time.sh
0
本帖最后由 sss 于 2015-6-2 13:47 编辑
- #!/bin/bash
- #When times are 0:00 and 12:00, you need the directory / data / log / file under the All Clear
- #While other times only statistics about the size of each file, a file line output to a name by date and time of the log
- #2015/05/16
- log=`date+ +%Y%m%d%H`.log
- while :
- time=`date +%H`
- dir=/data/log
- if [ $time -eq 0 ] || [ $time -eq 12 ]
- for F1 in `find $dir -type f`
- do
- >$F1
- done
- else
- find $dir -type f | xargs du -sh >/tmp/$log
- fi
- sleep 360
- done
0
aa=`date +%H`
bb=`find /data/log/ -type f`
cc=`date +"%F %T"`
case $aa in
00)
for i in `echo $bb`
do
echo " " > "$i"
done
;;
12)
for i in `echo $bb`
do
echo " " > "$i"
done
;;
*)
du -ah /data/log/* > "$cc".log
;;
esac
然后再做个定时任务,执行这个脚本
bb=`find /data/log/ -type f`
cc=`date +"%F %T"`
case $aa in
00)
for i in `echo $bb`
do
echo " " > "$i"
done
;;
12)
for i in `echo $bb`
do
echo " " > "$i"
done
;;
*)
du -ah /data/log/* > "$cc".log
;;
esac
然后再做个定时任务,执行这个脚本
0
#!/bin/bash while : ; do sleep 3600 a=`date` echo "$a" > 1.txt b=awk -F' ' '{ print $5 }' 1.txt|awk -F':' '{print $1}' c=00 d=12 if [ $b -eq $c -o $b -eq $d ] ; then find /root/zhai/shell -type f -exec cp -r /dev/null {} \; else e=`find /root/zhai/shell -type f -exec ls {} \;` for i in $e do echo "$e" > 1.txt h=`awk -F' ' '{print $9 ,$5}' 1.txt` echo " $h" >> $a.txt done fi done
0
#!/bin/bash
#writen by chenjie
date.H=`date +%H`
date.t=`date +%y%m%d%H`
if [ $date.H -eq 00] || [ $date.H -eq 12] ;then
dir=/tmp
for i in `ls $dir`
do
echo > /tmp/$i
done
else
du -sh /tmp/$i > /tmp/$date.t.log
fi
#writen by chenjie
date.H=`date +%H`
date.t=`date +%y%m%d%H`
if [ $date.H -eq 00] || [ $date.H -eq 12] ;then
dir=/tmp
for i in `ls $dir`
do
echo > /tmp/$i
done
else
du -sh /tmp/$i > /tmp/$date.t.log
fi
0
#!/bin/bash
while :
do
date=`date +%H`
if [ $date == 0 ] || [ $date == 12 ]
then
for i in `find /ddd -type f`
do
> $i
done
else
for i in `find /ddd -type f`
do
du -sh $i >> /tmp/`date +%Y-%m-%d_%H-%M-%S`.log
done
sleep 3600
fi
done
while :
do
date=`date +%H`
if [ $date == 0 ] || [ $date == 12 ]
then
for i in `find /ddd -type f`
do
> $i
done
else
for i in `find /ddd -type f`
do
du -sh $i >> /tmp/`date +%Y-%m-%d_%H-%M-%S`.log
done
sleep 3600
fi
done
0
Log_path=/data/log/
Log_file=/tmp/`date +%F\ %T.log`
D=`date +%H`
if [$D -eq 00 -o $D -eq 12 ];
then for l in `find $Log_path -type f`
do
>$l
done
else
find $Log_path –type f exec du –sh {}\; >$Log_file
fi
crontab -e
0 */1 * * * /bin/bash /tmp/linshi/2.sh
Log_file=/tmp/`date +%F\ %T.log`
D=`date +%H`
if [$D -eq 00 -o $D -eq 12 ];
then for l in `find $Log_path -type f`
do
>$l
done
else
find $Log_path –type f exec du –sh {}\; >$Log_file
fi
crontab -e
0 */1 * * * /bin/bash /tmp/linshi/2.sh
0
本帖最后由 大仔黑黑 于 2016-3-16 17:51 编辑
#!/bin/bash
##written by wangyl
##2016-03-16
dir=/data/log
log=`date +%F.log`
time=`date +%H`
cd $dir
if [ $time -eq 0 ] || [ $time -eq 12];then
for i in `ls`
do
if [ -f $i];then
echo "" > $i
fi
done
else
for a in `ls`
do
b=`du -h --max-depth=3 $a`
echo $b > $log
done
fi
crontab -e
* */1 * * * /bin/bash /usr/local/sbin/2016-03-16.sh
#!/bin/bash
##written by wangyl
##2016-03-16
dir=/data/log
log=`date +%F.log`
time=`date +%H`
cd $dir
if [ $time -eq 0 ] || [ $time -eq 12];then
for i in `ls`
do
if [ -f $i];then
echo "" > $i
fi
done
else
for a in `ls`
do
b=`du -h --max-depth=3 $a`
echo $b > $log
done
fi
crontab -e
* */1 * * * /bin/bash /usr/local/sbin/2016-03-16.sh
0
- #!/bin/bash
- # 定时功能在 crontab 添加
- # 0 * * * * sh 脚本路径
- now=`date +%H`
- path="/data/log/"
- if [[ $now -eq 0 || $now -eq 12 ]]
- then
- find $path -type f |xargs -i echo > {}
- else
- find $path -type f |xargs -i du -sb {} > /tmp/`date +%Y%m%d%H%M%S`
- fi
0
本帖最后由 HMOM 于 2016-3-20 22:49 编辑
#!/bin/bash
LOG_PATH=/data/log
LOG_FILE=/tmp/check_log.txt
CURR_H=`date +%H`
CURR_TIME=`date "+%F %T"`
[ ! -f $LOG_FILE ] || touch $LOG_FILE
if [ $CURR_H -eq 0 -o $CURR_H -eq 12 ];then
for log in `find $LOG_PATH -type f`
do
> $log
echo -e "######!!!!!!Empty log file in $CURR_TIME!!!!!!######\n\n" >>$LOG_FILE
done
else
echo -e "######$CURR_TIME check the log file size######" >>$LOG_FILE
for file in `find $LOG_PATH -type f`
do
CURR_SIZE=`du -sh $file | awk '{print $2"\t\t"$1}'`
echo -e "$CURR_SIZE" >> $LOG_FILE
done
echo -e "######check log file finished#########################\n" >>$LOG_FILE
fi
#!/bin/bash
LOG_PATH=/data/log
LOG_FILE=/tmp/check_log.txt
CURR_H=`date +%H`
CURR_TIME=`date "+%F %T"`
[ ! -f $LOG_FILE ] || touch $LOG_FILE
if [ $CURR_H -eq 0 -o $CURR_H -eq 12 ];then
for log in `find $LOG_PATH -type f`
do
> $log
echo -e "######!!!!!!Empty log file in $CURR_TIME!!!!!!######\n\n" >>$LOG_FILE
done
else
echo -e "######$CURR_TIME check the log file size######" >>$LOG_FILE
for file in `find $LOG_PATH -type f`
do
CURR_SIZE=`du -sh $file | awk '{print $2"\t\t"$1}'`
echo -e "$CURR_SIZE" >> $LOG_FILE
done
echo -e "######check log file finished#########################\n" >>$LOG_FILE
fi
0
#! /bin/bash
h=`date +%H`
d=`date +%Y%m%d%H`.log
##在0点和12点的时候清空/data/log/下的全部文件
if [ h -eq 0 || h -eq 12 ]
then
for f in `find /data/log/ -type f`
do
echo > $f
done
##其他的时间就统计文件的大小,再输出到一个按日期和时间的名字里
else
du -sh /data/log/* > /var/log/$d
fi
crontab -e
* */1 * * * sh myshell.sh
h=`date +%H`
d=`date +%Y%m%d%H`.log
##在0点和12点的时候清空/data/log/下的全部文件
if [ h -eq 0 || h -eq 12 ]
then
for f in `find /data/log/ -type f`
do
echo > $f
done
##其他的时间就统计文件的大小,再输出到一个按日期和时间的名字里
else
du -sh /data/log/* > /var/log/$d
fi
crontab -e
* */1 * * * sh myshell.sh
0
crontab -e
添加计划任务 * /1 * * * /bin/sh /usr/local/sbin/clear.sh
#!/bin/bash
H=`date +%H`
d=`date +"%F %T"`
dir=/data/log
b=`find $dir -type f |awk -F '/' '{print $NF}'`
exec >>/tmp/$d.txt
if [ $H -eq 0 -o $H -eq 12 ]
then
for i in `$b`
do
cat >$i <<EOF
EOF
done
else
du -sh $dir/*
fi
0
#Description: This script is to delete files that time specified.
#Author: Jiazhi Yang
#Date: 10/11/2016
#获取当前时间
CURTIME=date +%H:%M
echo $CURTIME
#获取当前日期
CURDATE=date -d -1day +%Y%m%d
#在/data/log找出相应的文件
FILES=find /data/log -type f
#让当前时间与0点和12点进行比较,符合条件就删除相应的日志文件内容,否则就统计文件大小
if [ $CURTIME = "00:00" ] || [ $CURTIME = "12:00" ]; then
for i in $FILES
do
cat /dev/null > $i
done
echo "Files deleted!!!" >>/tmp/$CURDATE-$CURTIME.txt
else
du -sh $FILES >>/tmp/$CURDATE_$CURTIME.log
fi
* */1 * * * /bin/bash /data/yangjz/exercise/delfile_20161110.sh
0
4 h=`date +%H`
5 d=`date +%F-%T`
6 if [ $h -eq 00 ] || [ $h -eq 12 ]
7 then
8 for i in `find /data/log -type f`
9 do
10 > $i
11 done
12 else
13 for i in `find /data/log -type f`
14 do
15 wc -c $i >>/data/$d.log
16 done
17 fi
0
#! /bin/bash
hour=`date +%H`
filename=`date +%d-%T`
if [ $hour -eq 0 -o $hour -eq 12 ]
then
find /tmp/1/ -maxdepth 1 -type f -exec rm {} \;
else
size=`find /tmp/1/ -maxdepth 1 -type f -exec du -h {} \;`
echo $size >> /tmp/$filename.log
fi
编辑回复