Scripting features: Move files from the specified directory to the target directory and keep the structure intact
Scenario: Used to remove old data (the author is an FTP server environment) that does not need to be retained in the server
Usage Note: Modify the source directory and target directory in the script and modify the data for the number of days before the move
Script code:
#!/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 days ago Data #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= "/";p rint} ' | 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= "/";p Rint} ') " fi if [ -d $j" ]; then echo s: "$i" echo d: "$j" mv -f "$i" "$j" if [ ! $? = 0 ]; then echo "$i" >> $mvlog fi fidoneifs= $oldIFS
This article is from the "Old Tan Linux Cluster blog" blog, please be sure to keep this source http://cmdschool.blog.51cto.com/2420395/1843962
File Move Script