69

Currently I have two directories A/ and B/ which are identical in every respect, with the exception of the timestamps. Therefore if I run the command :

rsync --dry-run -crvv A/ B/

then all files are marked "uptodate", whereas the command :

rsync --dry-run -rvv A/ B/

shows that all files are to be copied over from A/ to B/.

My question is this : given that I know the files are identical (in respect to contents), then is there any way (via rsync or otherwise) to set the timestamps for files in B/ to be identical to the timestamps of the files in A/, without copying over all the files from A/ to B/ ?

Thanks

artella
  • 1,099

8 Answers8

110

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
Scott Pack
  • 15,097
9

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.

6

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)

knorke
  • 61
4

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!:)

Stone
  • 7,279
1

Well, you could certainly write a script that reads the timestamp from one side and then uses touch to set it on same file on the other side.

But that would likely take you much longer than simply letting rsync try to copy all the files. Very little data will actually be transferred (only block hashes if the files are truly identical). But the full contents of every file will have to be read on each side at the least. So of you are limited by disk bandwidth or IOPS it could take a while. But still probably less time than writing and testing a script to do it.

rmalayter
  • 3,832
0

For people like me who intend to modify the files and want to retain the original timestamp (e.g. you update the meta tags of your music library).

It's based on the solution provided by Stone. However, here about the modification time and a working script to restore the timestamps. FIRST do step one, then start working with your files.

  1. Preserve old timestamps. It operates from the current directory, excludes all hidden files, and saves it to the temporary file in /tmp/files. You can always change parameters, but better stay with -printf '"%t" "%p"\n' since the later touch command utilizes that.

    find . ! -iname ".*" -printf '"%t" "%p"\n' -type f > /tmp/files

  2. Modify your files as much as you like

  3. now create a file that helps you restoring the timestamps:

    while read line; do echo touch -a -d $line >> job.sh; done < /tmp/times

  4. And finally apply the old dates to the modified files

    sh job.sh

Caveat: works for files with name spacing, special characters, but for instance no files with a $ sign in it.

Burcardo
  • 101
0

Best way to copy only timestamp on Windows :

To copy any file's last created/modified/accessed time stamp to another, you can use:

touch /c /m /a /r SourceFile DestFile

(This version of Touch is part of the Win32 Console ToolBox http://www.stevemiller.net/apps/ )

Robocopy "SOURCE_DIRECTORY" "DESTINATION_DIRECTORY" /E /COPYALL /DCOPY:DAT /XF . /R:10 /W:5 /V /ETA

Pierre
  • 101
-1

The rsync manual: man rsync lists a -t and -N switch, they might be worth fiddling with.

Tim
  • 3,087
  • 19
  • 15