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:
Copy all the
.docfiles from the remote server to my local~/Downloadsfolder:scp -i key.pem abc@xyz:/tmp/*.doc ~/Downloads/Select the latest file from
~/Downloadsand 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?
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.