檔案移動指令碼

來源:互聯網
上載者:User

標籤:備份   資料   移動   

指令碼功能:將指定目錄多少天前的檔案移動到目標目錄並保持結構不變

應用情境:用於移走伺服器中無需保留的老舊資料(筆者是ftp伺服器環境)

使用說明:修改指令碼中的來源目錄和目標目錄且修改移動多少天前的資料

指令碼代碼:

#!/bin/bashwdir="/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) directorcd $wdir#create source file listif [ -f $fl ]; then rm -f $flfifind -mtime +300 -type f | grep -v "\/\."> $fl#            ^300天前的資料#filter source file listif [ -f $okl ]; then rm -f $oklfiif [ -f $erl ]; then rm -f $erlfiif [ ! -f $fl ]; then echo stop:1 exit 1fioldIFS=$IFSIFS=$‘\n‘for i in $(cat $fl)do if [ -f $i ]; then  echo "$i" >> $okl else  echo "$i" >> $erl fidoneIFS=$oldIFS#create traget director listif [ -f $dl ]; then rm -f $dlfiif [ ! -f $okl ]; then echo stop:2 exit 2ficat $okl | awk -F‘/‘ ‘{$1="";NF-=1;OFS="/";print}‘ | sort -u > $dl#create traget directorif [ ! -f $dl ]; then echo stop:3 exit 3fioldIFS=$IFSIFS=$‘\n‘for i in $(cat $dl)do if [ ! -d "$bdir$i" ]; then  mkdir -p "$bdir$i"  if [ ! $? = 0 ]; then   echo "$bdir$i" >> $mdlog  fi  #echo $bdir$i fidoneIFS=$oldIFS#move file to traget folderif [ ! -f $okl ]; then echo stop:4 exit 4fioldIFS=$IFSIFS=$‘\n‘for i 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 fidoneIFS=$oldIFS


本文出自 “老譚linux叢集部落格” 部落格,請務必保留此出處http://cmdschool.blog.51cto.com/2420395/1843962

檔案移動指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.