To find the LATEST file from a dir on REMOTE machine and SCP to local machine?

Hi All,

URGENT - Please help me form a scipt for this:

I need the LATEST file from a dir on REMOTE machine to be SCP'd to a dir on local machine. (and I need to execute this from local server)

I know that the below cmd is used to find the LATEST file from a dir. But this command is not supported when used inside the SCP command.

To find LATEST file :

`ls -1r | head -1`

Thanks!
-UB

how about:

file_to_copy=`ssh username@hostname 'ls -1r | head -1'`
echo copying $file_to_copy ...
scp username@hostname:$file_to_copy /local/path

Thanks Yogesh; but no luck on this as well.

In precise, I have :

On SERVER1

dir1 contains:
file1
file2
file3 --> latest one

On SERVER2, I need to execute a script that fetchs file3 into dir2.

Thanks.

---------- Post updated at 02:58 PM ---------- Previous update was at 11:53 AM ----------

Yippiee!
After spending so much time doing R&D on this, I finally got an answer for myself! Hope this helps others as well.

On SERVER2, save this as a script and execute:

latest_file=`ssh IP_ADDR_OF_SERVER1 find /dir1 -type f -mtime -1 -name "file*"`
echo Staring SCP of $latest_file from SERVER1 to SERVER2...
/usr/bin/scp user@IP_ADDR_OF_SERVER1:$latest_file /dir2
echo SCP Completed.

Enjoy.

-UB

is there a way to get the files using FTP instead of SSH which is allowed inour source server ??

Thanks