Rsync : copying over timestamps only

Solution 1:

Using -t (preserve timestamps) and --size-only will only compare files on size. If the size matches, rsync will not copy the file but since -t is specified, it will update the timestamp on the destination file without recopying it.

Make sure to not use -u (update) as this will skip files that already exist and completely skip updating the timestamp.

I had the problem of originally not using rsync to copy a bunch of files to a new drive, and therefore the timestamps were updated to current time. I used the command below to sync everything correctly in a decent amount of time:

rsync -vrt --size-only /src /dest

Solution 2:

Using --size-only will cause rsync to skip comparing file timestamps (and therefore file contents) if the sizes match. Combining this with --times will clone the timestamps across to the target tree.

Solution 3:

I think rsync is the right tool to make this but if you want some script to do this than someting this can give a good start.

You can get a file list with timestamps (acces times) like this:

find . -printf '"%p" %a\n' -type f > /tmp/times

And get the appropriate time update commands from it:

 while read line; do echo touch -a -d \"${line#* }\" ${line%% *}; done < /tmp/times

It isn't a complete script but a good start place!:)

Solution 4:

Use the source (e.g. /path/to/source) directory as reference for the touch command. Just cd to your target directory and do

find -type f -exec touch -r /path/to/source/{} {} \;

You cannot copy sub-second timestamps with rsync (yet).

This also works for directories and pseudo files (just remove or change the -type f)