您现在的位置是:首页 > 文章详情

shell文件移动脚本

日期:2017-11-15点击:776

脚本功能:将指定目录多少天前的文件移动到目标目录并保持结构不变

应用场景:用于移走服务器中无需保留的老旧数据(笔者是ftp服务器环境)

使用说明:修改脚本中的源目录和目标目录且修改移动多少天前的数据

脚本代码:

#!/bin/bash
wdir= "/ftp"  #源目录
bdir= "/mvbackup/ftp"  #目标目录
 
fl= "/tmp/filelist"
okl= "/tmp/oklist"
erl= "/tmp/errlist"
dl= "/tmp/dirlist"
mvlog= "/var/log/mvfile.log"
mdlog= "/var/log/mkdir.log"
 
#enter working(source) director
cd  $wdir
 
#create source file list
if  [ -f $fl ];  then
  rm  -f $fl
fi
 
find  -mtime +300 - type  f |  grep  - v  "\/\." > $fl
#            ^300天前的数据
 
#filter source file list
if  [ -f $okl ];  then
  rm  -f $okl
fi
 
if  [ -f $erl ];  then
  rm  -f $erl
fi
 
if  [ ! -f $fl ];  then
  echo  stop:1
  exit  1
fi
 
oldIFS=$IFS
IFS=$ '\n'
for  in  $( cat  $fl)
do
  if  [ -f $i ];  then
   echo  "$i"  >> $okl
  else
   echo  "$i"  >> $erl
  fi
done
IFS=$oldIFS
 
#create traget director list
if  [ -f $dl ];  then
  rm  -f $dl
fi
 
if  [ ! -f $okl ];  then
  echo  stop:2
  exit  2
fi
cat  $okl |  awk  -F '/'  '{$1="";NF-=1;OFS="/";print}'  sort  -u > $dl
 
#create traget director
if  [ ! -f $dl ];  then
  echo  stop:3
  exit  3
fi
 
oldIFS=$IFS
IFS=$ '\n'
for  in  $( cat  $dl)
do
  if  [ ! -d  "$bdir$i"  ];  then
   mkdir  -p  "$bdir$i"
   if  [ ! $? = 0 ];  then
    echo  "$bdir$i"  >> $mdlog
   fi
   #echo $bdir$i
  fi
done
IFS=$oldIFS
 
#move file to traget folder
if  [ ! -f $okl ];  then
  echo  stop:4
  exit  4
fi
 
oldIFS=$IFS
IFS=$ '\n'
for  in  $( cat  $okl)
do
  if  [ -f  "$i"  ];  then
   j= "$bdir$(echo $i | awk -F'/' '{$1=" ";NF-=1;OFS=" / ";print}')"
  fi
  if  [ -d  "$j"  ];  then
   echo  s: "$i"
   echo  d: "$j"
   mv  -f  "$i"  "$j"
   if  [ ! $? = 0 ];  then
    echo  "$i"  >> $mvlog
   fi
  fi
done
IFS=$oldIFS

原文链接:https://www.centoschina.cn/shell/programming/9221.html
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章