linux - 比较2个目录并将差异复制到目录3

标签 linux comparison diff compare cp

我有三个目录。我想比较 directory1 和 directory2,然后将这些更改/新文件复制到 directory3。有没有一种简单的方法可以做到这一点,也许是通过使用 linux diff 和 cp 命令?我对想法持开放态度。

谢谢!

安德鲁

最佳答案

我相信这就是您想要从您的描述中得到的。

for file in dir2/*; do
    file_in_dir1=dir1/$(basename ${file})
    if [ ! -e  ${file_in_dir1} ]; then
        # If the file in dir2 does not exist in dir1, copy
        cp ${file} dir3
    elif ! diff ${file} ${file_in_dir1}; then
        # if the file in dir2 is different then the one in dir1, copy
        cp ${file} dir3
    fi
done

我不确定的一件事是,如果文件存在于 dir1 但不存在于 dir2 中,您想要什么。

关于linux - 比较2个目录并将差异复制到目录3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3267735/

相关文章:

Linux >2.6.33 : could sendfile() be used to implement a faster 'cat' ?

linux - "xargs -a file"将文件复制到文件夹

javascript - 如何计算字符串集合的哈希码(0..5000 个元素)

python - 字符串无法正确比较

comparison - 在算法之间选择

java - 如何使用java比较两个xml文件

tortoisesvn - TortoiseSVN:好的 'patch file'查看器?

linux - 在 Linux 中删除包含这两种模式的行

linux - 为什么链接器修改了--defsym "absolute address"

git - gitk 可以默认显示 merge 的差异吗?