bash - Moving a file and adding the date to the filename -


#!/bin/bash while read server <&3;   #read server names while loop       if [[ ! $server =~ [^[:space:]] ]] ;  #empty line exception     continue   fi      echo "connecting - $server"   #ssh "$server"  #ssh login   while read updatedfile <&3 && read oldfile <&4;          echo comparing $updatedfile $oldfile     if diff "$updatedfile" "$oldfile" >/dev/null ;       echo files compared same. no changes made.     else       echo files compared different.       # copy new file , put in right location       # make of old file , put in right location (time stamp)       # rm old file (not up)       #cp -f -v $newfile        # ****************************       mv $oldfile /home/u0146121/backupfiles/$oldfile_$(date +%f-%t)       # ****************************     fi    done 3</home/u0146121/test/newfiles.txt 4</home/u0146121/test/oldfiles.txt done 3</home/u0146121/test/servers.txt 

the line between * having trouble script. output file both date , filename. uses date. want both.

variable names may contain underscores, can't have underscores after bare variable names. in case you're using (undefined) variable $oldfile_ in destination file name, new name constructed "empty string + date". put variable name between curly brackets

mv $oldfile /home/u0146121/backupfiles/${oldfile}_$(date +%f-%t)

and renaming should work expected.


Comments