Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

#!/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

This is my whole script

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

This moves the file correctly but it removes the actualy file name and just adds the date. I want to keep the original file name and add the current date to the file name.

share|improve this question
    
What do you mean by "adding a timestamp"? You are renaming the file to the number of seconds since 1 Jan 1970 (that's what +%s means to date). –  choroba Jul 11 '13 at 14:40
    
@choroba updated the question –  mkrouse Jul 11 '13 at 14:45
    
you can use mv file /home/u0146121/backupfiles/file_$(date +%F) –  Rahul Patil Jul 11 '13 at 14:51
    
@RahulPatil that adds the date but doesnt keep the orginal file name as well –  mkrouse Jul 11 '13 at 15:16
    
mv file /dest/$_$(date +%F) –  Rahul Patil Jul 11 '13 at 15:20
add comment

marked as duplicate by Gilles, slm, jasonwryan, manatwork, jofel Jul 12 '13 at 16:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

Try mv $oldfile $dest_dir/$oldfile_$(date +%F-%T).

mv $oldfile /home/u0146121/backupfiles/$_$(date +%F) works in a one-liner if you manually replace $oldfile with the name of the file, but if you're specifically referencing the variable $oldname, $ will skip $oldfile as an argument and reach back into the history to get the last argument.

share|improve this answer
    
That doesnt work –  mkrouse Jul 11 '13 at 15:23
    
Are you using this in a script or as a one-liner? What does it give you as output? What OS are you using? –  aetimmes Jul 11 '13 at 15:26
    
no its part of a bigger script. the output is this 2013-07-11-10:22:25 i need to keep the original filename as well –  mkrouse Jul 11 '13 at 15:28
    
i will post my whole script for you if you want –  mkrouse Jul 11 '13 at 15:30
    
Can you paste the script anywhere? –  aetimmes Jul 11 '13 at 15:31
show 3 more comments

Not the answer you're looking for? Browse other questions tagged or ask your own question.