I have a backup software that creates folders every x amount of hours. like this:


(the name of the folder has the timestamp, and that is also given by the "last modified" property on the file system.

I want to rsync the most recent folder (including all its files and subfolders)

  • rsync to where? – roaima Apr 25 '17 at 16:58
  • Do you delete backups from the target before you delete them from the source? If not, why can't you just rsync everything and let it skip items that have already been copied successfully? – roaima Apr 25 '17 at 17:00
  • rsync to another server/folder etc, I dont think it matters. – Dotty Apr 25 '17 at 19:09
  • I dont delete the backups from the target, the folder gets overwritten always, with the latest folder, thats why i want to rsync only the latest one, (the folder structure inside those folders in the example above is always the same) – Dotty Apr 25 '17 at 19:11
  • So you have a fixed target directory that is to contain the contents of the most recent local backup? And is this target directory local or remote? This is important. – roaima Apr 25 '17 at 20:02

In your case, the most recent directory is the one whose name comes last in lexicographic order. So it's the last element in the expansion of * or any other pattern that matches the directory names.

sync_latest_dir () {
  eval "last=\${$#}"
  rsync -a "$last" remote-host:/some/where/
sync_latest_dir [0-9][0-9][0-9][0-9][0-9][0-9]-*
| improve this answer | |
  • Wouldn't running rsync on the parent directory with the --ignore-existing flag achieve the same thing? – Mio Rin Apr 26 '17 at 0:27
  • @Mioriin Usually, yes, but according to the comments under the question this isn't desired here. – Gilles 'SO- stop being evil' Apr 26 '17 at 0:29

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.