Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I get the only the latest files from a Unix server to another Unix server?

I have connected via SFTP to server 2 and trying to apply a for loop to check the time stamp, which is not working:

sftp $SERVER2  << !EOF
cd $Server2_FILE_LOCATION
echo Pwd File location: $pwd

LastUpdatedTS=$(grep "value of TimeStamp is" /root/airtelSnD/BoTreeScript/BotreeLastFileTS.txt | cut -d'=' -f2)

echo 1

for file in mad_*.*
do
CurrentFileTS=$(stat -c %Y $file |awk '{print  strftime( "%Y%m%d%H%M%S", $1 )}')

echo 2

echo TS of last updated file is : $LastUpdatedTS
echo value CurrentFileTS is $CurrentFileTS

echo 3

if [[ $CurrentFileTS -gt $LastUpdatedTS ]]
 then
    echo if......
    mget  $file $DESTINATION_SERVER
    echo value of TimeStamp is=$CurrentFileTS > $LASTFILE_TS    
else
    echo else...       
 fi
done

quit
!EOF
share|improve this question
    
I agree with the posted answers that you should use a tool that is designed to do this task. But if you want to know how to get scripts like this to work, try putting a backslash before all dollar sign symbols except for the ones that are using a value from the sftp environment. (It looks like $Server2_FILE_LOCATION and $DESTINATION_SERVER are the only ones to which that applies.) –  Scott Dec 13 '12 at 15:57
add comment

2 Answers

Have you tried rsync?

rsync is an open source utility that provides fast incremental file transfer.

It will only transfer changed/new files, and only deltas for changed files. It also has various other nifty features.

This doesn't really address any issues with your code, but I think rsync will be easier than trying to reinvent the wheel with SFTP.

share|improve this answer
    
Nope,as per my requirement I dont have to use any tool. I need to do it through scripts only. –  Maddy Dec 28 '12 at 9:29
add comment

Rsync (or Unison) is best

Another solution would be to use find

 find . -name 'mad_*' -newer last_update -exec sftp ... {} \;
 touch last_update
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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