How can I script making a backup of recently modified files in Bash?

A way would be to modify the file names that you get when running find. So in your loop, having a matched file name in $filename you shall:

  1. In $filename, replace the leading $i with nothing
  2. find out the $dir_name in the result of 1 by using dirname
  3. append a trailing $copyLocation to $dirname and use it as argument for mkdir -p to create missing directories
  4. copy $filename to "$copyLocation/$dirname"

I'm also going to suggest yet another file sync alternative: unison. It is easier to use than rsync.


rsync is made for this task. Check the examples page for usage.


A little off-topic perhaps, but you may want to take a look at BoxBackup. I used rsync scripts for a long time before moving to BoxBackup, and it really makes things easier - especially the "housekeeping"...