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?

  • Have you tried to use rsync to do this? – VaTo May 28 '15 at 19:25
  • (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. – G-Man Says 'Reinstate Monica' May 28 '15 at 22:20
  • (1)Thanks for the edit. I have retained the edited version. – alex May 29 '15 at 13:14
  • Current working directory is a normal folder within my file system. This is answered by shivams. by the usage of . – alex May 29 '15 at 13:15
  • And yes, will use the quotes. Did not know about it. thanks. – alex May 29 '15 at 13:15

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 .
| improve this answer | |
  • 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 – alex May 29 '15 at 13:26

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".

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.