copy only newer files? (xcopy equivalent)

Howdy folks.

I have a problem - I'm sure the answer is very simple, but I can't work it out.

I want to create a UNIX shell script that does what I've been doing in DOS batch files for years - that is, backing up files. By which I mean copying files from a source directory to a target directory, only if a) the file doesn't exist at the target, or b) the file does exist but is older than the source.

In DOS, I did something like this:

xcopy c:\path\directory\*.* x:\backup\ /d /e

Where x was a networked drive, /d meaning only copy files newer than the target, /e meaning recurse into subdirectories.

In UNIX, I'm close but no cigar yet... I have the following:

cp -r /testdir/source/ /testdir/target/

This works in that it copies files, leaving the originals behind and recursing into subdirectories, but it doesn't only copy source files if they're newer than the target. It copies eveything.

I have read in a few different places that cp accepts the -u ('update' I think) option, to make it only copy newer files, but I can't get this to work. If I write cp -u, I am told that 'u' is an 'illegal option' for cp.

So, is there a way to get the behviour I'm after? Am I right to be using cp, or is there a better function to do what I want?

Doing all this on Mac OS 10.3.9, using the terminal, tcsh.

Very grateful for any help.

Cheers.

gnu copy (as is on linux) has the -u option.
Also have a look at rsync

hmm tcsh, tough, i didnt ever use it.
If you want to go with ksh tho, i ll use something like this

tgt is the target dir

for i in *
do
newf=`find . -name $i -newer "../tgt/$i"`
if [ "x$newf" != "x" ]
then
cp $newf ../tgt/$i
fi
done

hope this gives you some pointers

Thanks to both.

rsync seems to do the job, but I'm still having some issues, such as certain files not being copied - I get this:

mkstemp "path/filename.jNLmL7" failed: Invalid argument

...which is double dutch to me. Any ideas about this?

The files that do copy seem to lose their application association too, which is not a deal breaker, but is a bit of a PITA. I suspect this is something to do with me moving files around behind the Finder's back. I might chase this bit up on the Apple forums.

Cheers!

Aha - After Googling to find out what mkstemp is, I've discovered that the error is due to the file name length. Shortening the file name has solved the problem - the temp file name with the .XXXXXX extension must have been pushing it over the OS's file name length limit. (I guess)