file transfer using scp..

Hi Frdz

I have a problem like.

I need to transfer a file from source to destination (different systems with different IPs) using "scp" command and before transfer the file i have to check the file is available in destination or not, if it is there no need to transfer, otherwise we have to transfer.

Can anyone do the needful.

Thanx

first check if the file on remote machine exists or not

check=`ssh -n -l $USER $DESTIP " ls $PATH_FILE | wc -l"`

if [ $check == 0 ]; then
# do your file transfer using scp
else
echo " file is alraedy present"
fi

Or even simpler:

ssh user@host "test -e $FILE"
if [ $? -eq 0 ]; then
    # Transfer
elif [ $? -eq 255 ]; then
    # SSH error
else
    # Skip
fi

Hi,

Check is returning 1 even the file exists or not..wat to do
Thanx

you should be careful whether the command : ssh user@host "test -e $FILE" returns 0 for successful execution of ssh command or successful test of the file exists. check your SSH manual for any such indications.

O'Rly? (Tested on Linux & HP-UX)

$ ssh host1 'test -e /etc/resolv.conf' ; echo $?
0
$ ssh host1 'test -e /etc/resolv.confa' ; echo $?
1
$ ssh host1a 'test -e /etc/resolv.conf' ; echo $?
ssh: host1a: Name or service not known
255
$ ssh host1a 'test -e /etc/resolv.confa' ; echo $?
ssh: host1a: Name or service not known
255
$ ssh user@host2 'test -e /etc/resolv.conf' ; echo $?
ksh: test: argument expected
1
$ ssh user@host2 'test -f /etc/resolv.conf' ; echo $?
0
$ ssh user@host2 'test -f /etc/resolv.confa' ; echo $?
1

OpenSSH always passes back the return code of the command, your only problem might be if the command returns 255, since that would make it difficult to discern between an SSH error and an application error.

Maybe your hosts' test command doesn't know about the -e switch. Use -f instead (which doesn't check for existence but whether it's a file or not. Will fail on directories)