查找最近更新文件的shell脚本


点击(此处)折叠或打开

  1. #!/bin/ksh
  2. ########################################################
  3. #Date: 2012-12-19
  4. #name: swiftftp.sh
  5. #decription: 无限循环,每1分钟执行一次,查找当前目录中修改时间超过1分钟但未超过
  6. # 2分钟的文件,ftp到前置上
  7. ########################################################


  8. datestr=`date "+%Y%m%d"`
  9. _logname=dynamicftp${datestr}.log
  10. chmod +x *
  11. if [[ ! -f ${_logname} ]];then
  12.     touch ${_logname};
  13. fi

  14. #该函数打印参数1的时间戳
  15. # perl usage: perl -e "$a=1+4;exit($a);"
  16. cal_timestamp()
  17. {
  18.    perl -e '
  19.     use Time::Local;

  20. # get current time
  21. # 参数表示,要求返还minu_num个分钟之前的时间戳yyyymmddhhmm.ss
  22.     $minu_num="$ARGV[0]";
  23.           localtime(time);

  24.        $time1= time -60*$minu_num;
  25.        ($sec,
  26.           $min,
  27.           $hr,
  28.           $mday,
  29.           $mon,
  30.           $yr,
  31.           $wday,
  32.           $yday,
  33.           $isdst)=localtime($time1);
  34.   
  35.         $yr=$yr+1900;
  36.         $mon=$mon+1;
  37.         $beforetime=sprintf("%d%02d%02d%02d%02d.%02d",$yr,$mon,$mday,$hr,$min,$sec);
  38.         print "$beforetime"
  39.         ' "$1" #把函数的第一个参数,用这种形式传递给perl
  40. }


  41. #最外层循环
  42. while [[ 1 -eq 1 ]] ; do
  43. # example use of cal_timestamp:
  44.     oneminubef=$( cal_timestamp "1")
  45.     echo "1 minutes before:$oneminubef" >> ${_logname}
  46.     twominubef=$( cal_timestamp "2")
  47.     echo "2 minutes before:$twominubef" >> ${_logname}
  48.     
  49.     # create two older files
  50.     touch -t $oneminubef oneminubef.tmp
  51.     touch -t $twominubef twominubef.tmp
  52.     
  53.     #find the file list which is younger than 2 minutes but older than 1 minutes
  54.     #find usage: find -newer file1 ! file2
  55.     
  56.     touch tempfile01
  57.     chmod 777 tempfile01
  58.     find . -newer twominubef.tmp ! -newer oneminubef.tmp | grep -v '^./oneminubef.tmp' |grep -v "^./${_logname}" |grep -v "{$0}" > tempfile01
  59.     
  60.     #read the file and strcat the files
  61.     a=0
  62.     filenames=
  63.     while read line
  64.     do
  65.       line=`echo $line | sed -e 's/^ *//' -e 's/ *$//'` #去除空格
  66.       length=`echo ${line} | awk '{print length($1);}'` #计算字符数
  67.       if [[ ${length} -gt 0 ]]; then #防止空行
  68.         newline=${line#*/} #去除文件名前面的./
  69.         filenames=$filenames" "$newline
  70.         a=`expr $a + 1`
  71.       fi
  72.     done < tempfile01
  73.     rm -rf tempfile01 oneminubef.tmp twominubef.tmp
  74.     
  75.     echo "files :$filenames" >> ${_logname}
  76.     
  77.   
  78.     _username=test
  79.     _passwd=test
  80.     
  81.     if [[ $a -gt 0 ]]; then
  82.         ftp -in 192.168.93.77 << !
  83.                 user ${_username} ${_passwd}
  84.                 bi
  85.              mput $filenames
  86.                 bye
  87. !
  88. fi
  89.         
  90. if [[ $? -eq 0 && a -gt 0 ]]; then
  91.                     echo `date`" successfully put $a files on server" >> ${_logname}
  92. elif [[ a -gt 0 ]]; then
  93.         echo `date`" put files error!" >> ${_logname}
  94.     else
  95.         echo `date`" no files need to be put!" >> ${_logname}
  96.     fi
  97.     sleep 60
  98. done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值