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

20.3k views Asked by At

I have a lot of files from one side (A) and a lot of other files in other place (B)

I'm copying A to B, there are a lot of files are the same, but content could be different!

Usually I used mc (Midnight Commander) to do it, and selected "Overwrite if different size". But there is a situation when size are the same, but content is different. In this case mc keeps file in B place and not overwrite it.

In mc overwrite dialog there is a work "Update" I don't know what it is doing? In help there is no such information, maybe this is a solution?

So I'm searching solution which can help me copy all files from A to B and overwrite files in B place if they exists AND content is different from A.

if file in "B" place exists (the same name) and content is different it has to be overwritten by file from "A" place every time.

Do you know any solution?

4

There are 4 answers

1
Didier Trosset On BEST ANSWER

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.

2
trojanfoe On

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.

0
Gevorg On

http://linux.math.tifr.res.in/manuals/man/mc.html

The replace dialog is shown when you attempt to copy or move a file on the top of an existing file. The dialog shows the dates and sizes of the both files. Press the Yes button to overwrite the file, the No button to skip the file, the alL button to overwrite all the files, the nonE button to never overwrite and the Update button to overwrite if the source file is newer than the target file. You can abort the whole operation by pressing the Abort button

1
Grmpfhmbl On

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.