I'm doing this sync locally on Ubuntu 12.04. The files are generally small text files (code).
I want to copy (preserving mtime stamp) from source
directory to target
but I only want to copy if the file in target
already exists and is older than the one in source
.
So I am only copying files that are newer in source
, but they must exist in target
or they won't be copied. (source
will have many more files than target
.)
I will actually be copying from source
to multiple target
directories. I mention this in case it impacts the choice of solution. However, I can easily run my command multiple times, specifying the new target
each time, if that's what is required.
Best Answer
I believe you can use
rsync
to do this. The key observation would be in needing to use the--existing
and--update
switches.A command like this would do it:
Example
Say we have the following sample data.
Which looks as follows:
Now if I were to sync these directories nothing would happen:
If we
touch
a source file so that it's newer:Another run of the
rsync
command:We can see that
file3
, since it's newer, and that it exists indst/
, it gets sent.Testing
To make sure things work before you cut the command loose, I'd suggest using another of
rsync
's switches,--dry-run
. Let's add another-v
too sorsync
's output is more verbose.