This discussion is archived
1 Reply Latest reply: Apr 15, 2013 12:36 PM by Alan_S RSS

How to cp the latest file from a remote server?

watson2011 Newbie
Currently Being Moderated

I want to cp the latest[ls -tr *abc_File | tail 01] file from a remote server. What is the best way to do this? Thank you.

I got an error, 'No such file or directory' from below line:
scp user@$( ls -tr _abcde.txt | tail -1 ) /u01/abc/defg/

I also received an error with this line:
ssh user@ "scp /export/home/abc/$( ls -tr *_abcde.txt | tail -1 ) /u01/abc/defg/"
  • 1. Re: How to cp the latest file from a remote server?
    Alan_S Explorer
    Currently Being Moderated
    The shell command substitution you are trying to perform is executed on the local sytem: $(ls -ltr ...). If you want to execute the command on the remote system, then scp cannot do this. You would have to use something like ssh to the remote machine, and execute scp from there. I would recommend a two-part shell script:

    newest=$(ssh user@ ls -tr /export/home/abc/*_abcde.txt | tail -1 )
    scp user@$newest /u01/abc/defg/

    -- Alan


  • Correct Answers - 10 points
  • Helpful Answers - 5 points