Copying latest file from remote server

7,970

Solution 1

I am not really clear what your problem is, but if you're trying to copy to the current directory then just use . to refer to the current directory so that your command is:

scp -i key.pem abc@xyz:/tmp/*.doc .

Solution 2

Let's see if this will solve your problem. Make a script.

FILE=`ssh abc@xyz ls -ht /tmp/*.doc | head -n 1`
rsync -avz -e ssh abc@xyz:"$FILE" .

This will run a command on the remote server "ls -ht /tmp/*.doc | head -n 1" and will show the latest doc. Rsync that specified file from remote server to your current directory. ssh/rsync will also ask you a password for the user "abc", better is to use a passwordless login or a keyfile "key.pem".

Share:
7,970

Related videos on Youtube

alex
Author by

alex

Updated on September 18, 2022

Comments

  • alex
    alex almost 2 years

    There is a collection of .doc files, in addition to other types of files, on a remote server (which supports SCP).

    I am trying to write a script to retrieve the latest (most recently modified) .doc file from the remote server. The path to my current working directory cannot be absolute since my script may be deployed in another server.

    I am able to solve the problem partially in two steps:

    1. Copy all the .doc files from the remote server to my local ~/Downloads folder:

      scp -i key.pem abc@xyz:/tmp/*.doc ~/Downloads/
      
    2. Select the latest file from ~/Downloads and copy it to the required folder:

      cd ~/Downloads
      latest_file=$(ls -t *.doc | head -n 1)
      cp -p "$latest_file" /current working directory
      

    How can I copy the latest .doc file present in the remote server xyz under the folder /tmp to my local machine in a single statement without downloading all of them into an intermediate folder?

    • VaTo
      VaTo about 9 years
      Have you tried to use rsync to do this?
    • G-Man Says 'Reinstate Monica'
      G-Man Says 'Reinstate Monica' about 9 years
      (1) I’ve made a rough guess at what you mean, and edited your question based on that guess.  If I damaged the meaning, please fix it.  (2) I don’t understand what you’re saying about your current working directory, either.  (3) When you’re using a deferred wildcard (e.g., a wildcard that you want to be expanded on a remote server), you should always enclose it in quotes; e.g., scp -i key.pem "abc@xyz:/tmp/*.doc" ~/Downloads.  It’s true that it will work without the quotes 99% of the time, but it you just get into the habit of using quotes all the time, you’ll be protected in that 100th case.
    • alex
      alex about 9 years
      (1)Thanks for the edit. I have retained the edited version.
    • alex
      alex about 9 years
      Current working directory is a normal folder within my file system. This is answered by shivams. by the usage of .
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' about 9 years
    Note that the OP indicated that he was using a key.pem file in the question.
  • alex
    alex about 9 years
    The dot (.) solved my problem of having the files copied to my local working directory. Thank you. One pending question now. How will I copy only the latest file from the remote location instead of the whole set of *.doc files