How to determine if cp -u actually copied the file

cptimestamps

I'm looking for a concise way to check if a file was copied using cp -u.

I have a little shell script which updates quite a number of files. At the moment the script echoes a little message for each file which gets copied.

Now I want to change the script in such a way that the echo only gets executed when the file actually was updated. I've checked if cp -u returned an error code if it didn't copy the file, but this isn't the case.

Is there a better way than manually comparing the timestamps of the files?

Best Answer

if [ "$(cp -uv source destination)" != "" ]; then echo copied; else echo not copied; fi

Update

Match "->" in cp's verbose output. It only occurs if it could be successfully copied and if your filenames do not contain "->".

if [[ "$(cp -uv source destination)" =~ \-\> ]]; then echo copied; else echo not copied; fi
Related Question