0

Attempting to discover a command to copy files if the destination's (not source's) file has not been modified in the last hour.

  • 1
    There isn't a direct one. Could you modify your requirement to copy files only if the source is newer (has been modified more recently) than the target? – roaima Jul 25 '18 at 21:58
  • Sadly not. Is it possible to move files that have been modified in the last hour? If that is the case, I could do that, copy all the files then move the moved files back into their original location. – James Jul 25 '18 at 22:01
0

I know of no command that will precisely match your requirement. Something like this should work (remove the --dry-run when you're sure you're happy with the result; replace the --verbose with --quiet if you want it to run more silently):

src=/path/to/source
dst=/path/to/target

comm -z -23 \
        <(find "$src" -type f -printf '%P\0' | sort -z) \
        <(find "$dst" -type f -mmin -60 -printf '%P\0' | sort -z) |
    rsync --dry-run --verbose --archive --from0 --files-from - "$src" "$dst"

It assumes relatively recent utilities that understand how to handle NUL-terminated lines. If necessary, and provided that you can guarantee that no filenames contain newlines, you could remove the three -z flags and rsync's --from0 and replace the \0 in the find commands with \n.

  • Awesome. Thank you! Not sure if this works yet, will give it a go when this is required. – James Jul 25 '18 at 22:36
0

If all your copies are to existing files, you can look at it the other way: overwrite local files if they are older than one hour. So, with the destination directory as the working directory:

find . -mindepth 1 -maxdepth 1 -type f -mmin +60 -exec cp /source/directory/{} . \;

where:

  • find . -mindepth 1 -maxdepth 1 -type f -mmin +60 finds the local files that are over an hour old
  • -exec cp /source/directory/{} . \; overwrites them with their namesake from /source/directory
  • Thank you! I will give this a shot if roaima's answer doesn't work. – James Jul 25 '18 at 22:36

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.