Bashでファイルを移動するときにタイムスタンプを追加する

Bashでファイルを移動するときにタイムスタンプを追加する
#!/bin/bash
while read server <&3; do   #read server names into the while loop    
 if [[ ! $server =~ [^[:space:]] ]] ; then  #empty line exception
    continue
 fi   
echo "Connecting to - $server"
#ssh "$server"  #SSH login
    while read updatedfile <&3 && read oldfile <&4; do     
        echo Comparing $updatedfile with $oldfile
        if diff "$updatedfile" "$oldfile" >/dev/null ; then
            echo The files compared are the same. No changes were made.
        else
            echo The files compared are different.
            # copy the new file and put it in the right location
            # make a back up of the old file and put in right location (time stamp)
            # rm the old file (not the back 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

これは私の完全なスクリプトです。

mv $oldfile /home/u0146121/backupfiles/$_$(date +%F)

これによりファイルは正しく移動されますが、実際のファイル名は削除され、日付のみが追加されます。元のファイル名を保持し、ファイル名に現在の日付を追加したいと思います。

ベストアンサー1

努力するmv $oldfile $dest_dir/$oldfile_$(date +%F-%T)

mv $oldfile /home/u0146121/backupfiles/$_$(date +%F)$oldfileファイル名に手動で置き換えると、1行は機能しますが、変数を具体的に参照する場合は引数に$oldnameスキップ$し、$oldfile最後の引数の履歴として返されます。

おすすめ記事