0

Using Bash, I need to copy and override some files that have equal names but different contents from source, while ignoring equal files.

I saw no option in cp's manual for copying only differing files.

First I tried using cp's "update" option but it won't copy older files from source to destination.

cp -a -u SOURCE DESTINATION

If I remove the -u parameter it does copy all files correctly, but I want to avoid unnecessary copies.

I need something like this:

  1. Source files must be copied when the destination is different from source.
  2. Sometimes the destination is newer than source. Still, the copy must be done.
  3. Only when source files are equal to the destination, they must be ignored.

How should I do that?

Panki
  • 6,664
markfree
  • 365
  • You are not using bash in your example - while your script may be interpreted by bash, you are using cp to copy files. – Panki Jan 07 '22 at 19:21

2 Answers2

6

cp does not compare file contents, only timestamps.

Use rsync instead:

rsync -acv SOURCE/ DEST/

To get less output, drop the v flag.

Panki
  • 6,664
-1

You could apply one of the many available checksum commands. For this simple case, a simple algorithm might suffice. If the checksum compare equal, probability the two file being identical is high.

RudiC
  • 8,969