I'm trying to copy the contents of the newest directory from a remote server to a local server. Problem is that the directory names are date based, so it can't be static value in the command. I'd like to run the command from a cron as well so I'm hoping this is possible without some script.

Remote directory example (ls):


So in this case, I'd like to copy the contents of the 2017-09-25 directory as that is the newest and both name and modified stamp are same date.

To give a visual example:

rsync -chavzP --stats root@ /directory/backups/(ls -td -- */ | head -n 1) /my_local_dir

Is there a way to do this with rsync or even scp?


I realize this may seem like a trivial task. However, there are several reasons why I am seeking a way to do the copy in this manner. I've searched quite a bit but have not found anything to make it clear to me how to write the command to copy using either rsync or scp or whether it is even possible. My command-line-fu is basic.

  • why don't you just run rsync /directory/backups/*? rsync only copies new and modified files anyway. – Gerald Schneider Sep 27 '17 at 7:27
  • @GeraldSchneider - Because I'd end up with a folder for every day of the month on the local system, and have to create something to clean things up. Locally the backups will be on a snapshot schedule. – Chris Sep 27 '17 at 14:40
  • It sure would be helpful to get a comment with a downvote on a question. – Chris Apr 16 '19 at 2:47

I would run a script locally on the source server and have it push the backup to the destination server. The main reason for this that I get access to all of the relevant scripting tools to be able to work out what needs to be sent. An additional benefit is that I don't need to use a root account with ssh keys and no passphrase (as per your example) as I can use an unprivileged account to do the push.

| improve this answer | |
  • Thanks for your opinion. However, Remote is static. Local is dynamic. I can always hit remote from local and not have to add dns/ip management in the mix. There are other reasons as well. – Chris Sep 27 '17 at 14:55

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.