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

4

18

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?

Liverpudlian answered 27/10, 2011 at 10:26 Comment(1)
btw, all files are plain text filesLiverpudlian
A
19

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.

Agma answered 27/10, 2011 at 10:31 Comment(1)
it seems that this is what I need -u means copy only when the SOURCE file is newer than the destination file or when the destination file is missing but what timestamp it looks for ? record time ? and realy it's not ideal solution but maybe easiest for now, thanks.Liverpudlian
L
24

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.

Loree answered 29/5, 2016 at 14:48 Comment(1)
The -c switch is the key here. I wanted to avoid the "modified" timestamp from changing and compare on file content rather than just timestamps. Thanks!Semen
A
19

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.

Agma answered 27/10, 2011 at 10:31 Comment(1)
it seems that this is what I need -u means copy only when the SOURCE file is newer than the destination file or when the destination file is missing but what timestamp it looks for ? record time ? and realy it's not ideal solution but maybe easiest for now, thanks.Liverpudlian
S
0

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.

Stichous answered 27/10, 2011 at 10:32 Comment(2)
do you know any software which can do something similar ? 1. make hash for source file tree. 2. make hash for destination files tree. 3. compare it and copy only changed files. r ight ?Liverpudlian
Not off hand, no, but it sounds like a fun thing to write yourself using the openssl library :)Stichous
T
0

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

Tutt answered 28/4, 2018 at 10:53 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.