Linux 将 IF 内容复制到其中时覆盖文件不一样

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7914603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:53:28  来源:igfitidea点击:

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

linuxdeploymentconsolecopyingmc

提问by user1016265

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

我在一侧有很多文件 (A),在其他地方有很多其他文件 (B)

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

我正在将 A 复制到 B,有很多文件是相同的,但内容可能不同!

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.

通常我使用mc(Midnight Commander)来做,并选择“如果不同大小则覆盖”。但也有大小相同,内容不同的情况。在这种情况下, mc 将文件保存在 B 位置而不是覆盖它。

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?

在 mc 覆盖对话框中有一个工作“更新”我不知道它在做什么?在帮助中没有这样的信息,也许这是一个解决方案?

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.

所以我正在寻找可以帮助我将所有文件从 A 复制到 B 并覆盖 B 中的文件(如果它们存在且内容与 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.

如果“B”位置的文件存在(同名)且内容不同,则每次都必须被“A”位置的文件覆盖。

Do you know any solution?

你知道有什么解决办法吗?

采纳答案by Didier Trosset

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.

应该递归地将所有更改的文件(最近的时间戳)从目录 A 复制到目录 B。

You can also use -ainstead of -rin the command line, depending on what you want to do. See the cpman page.

您也可以在命令行中使用-a而不是-r,这取决于您想要做什么。请参阅cp手册页。

回答by trojanfoe

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.

您可能希望保留某种“索引”文件,其中包含您在编写文件时创建的文件的 SHA-1 哈希值。然后,您可以计算“源”哈希并将其与索引文件中的“目标”哈希进行比较。这仅在此过程是将文件写入目标的唯一方式时才有效。

回答by Grmpfhmbl

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

我会使用,rsync因为这不会依赖于文件日期,而是实际检查文件的内容是否已更改。例如:

#> 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).

Rsync 将文件复制到远程主机或从远程主机复制,或在当前主机本地复制(它不支持在两个远程主机之间复制文件)。

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

See man rsyncfor more options and details.

有关man rsync更多选项和详细信息,请参阅。

回答by Gevorg

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

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

当您尝试在现有文件的顶部复制或移动文件时,会显示替换对话框。该对话框显示了两个文件的日期和大小。如果源文件比目标文件新,按是按钮覆盖文件,否按钮跳过文件,所有按钮覆盖所有文件,nonE 按钮从不覆盖和更新按钮覆盖。您可以通过按 Abort 按钮来中止整个操作