Overwrite file in copying IF content to of them not the same Overwrite file in copying IF content to of them not the same linux linux

Overwrite file in copying IF content to of them not the same


I'd use rsync as this will not rely on the file date but actually check whether the content of the file has changed. For example:

#> rsync -cr <directory to copy FROM> <directory to copy TO>

Rsync copies files either to or from a remote host, or locally on the current host (it does not support copying files between two remote hosts).

-c, --checksum    skip based on checksum, not mod-time & size-r, --recursive   recurse into directories

See man rsync for more options and details.


Have you tried the command line:

cp -ru A/* B/

Should copy recursively all changed files (more recent timestamp) from directory A to directory B.

You can also use -a instead of -r in the command line, depending on what you want to do. See the cp man page.


You might want to keep some sort of 'index' file that holds the SHA-1 hash of the files, which you create when you write them. You can then calculate the 'source' hash and compare it against the 'destination' hash from the index file. This will only work if this process is the only way files are written to the destination.