copy the latest file in the remote server's directory

Hi Expert Team,
I performed the below piece of code to copy the latest file in the remote server's directory to the same server's other directory.
But it is not working properly.

How can i handle this? Can you please help me..?

ssh ${REMOTE_USERID}@${REMOTE_HOSTNAME} "cp -p `ssh ${REMOTE_USERID}@${REMOTE_HOSTNAME} "ls -tr1 ${SOURCE_FILE} | tail -1"` ${TARGET_FILE}"

Thanks,
Kanda

what error did you get?

Hi Yogesh,

I didnt get any error, but the file is not copied to the target directory of the remote server.

Thanks,
Kanda

Have you tried using scp instead of a nested ssh command? Here's an example below (without any ${} or {$})

### This is one line - broken down to improve readability
scp -rp REMOTE_USERID@REMOTE_HOSTNAME:SOURCE_FILE`ssh 
REMOTE_USERID@REMOTE_HOSTNAME ls -tr1 /SOURCE_FILE | tail -1` 
REMOTE_USERID@REMOTE_HOSTNAME:TARGET_FILE

I tested by running the following from myhost1:

scp -rp avronius@myhost2:/opt/home/avronius/misc/`ssh 
avronius@myhost2 ls -tr1 /opt/home/avronius/misc/ | tail -1` 
avronius@myhost2:/opt/home/avronius/misc/newfile