比较2个目录并将差异复制到目录3
我有三个目录。我想将目录 1 与目录 2 进行比较,然后将这些更改/新文件复制到目录 3 中。有没有一种简单的方法可以做到这一点,也许通过使用 linux diff 和 cp 命令?我对想法持开放态度。
谢谢!
安德鲁
I have three directories. I would like to compare directory1 with directory2, then take those changes/new files and copy them over to directory3. Is there an easy way to do this, maybe by using linux diff and cp commands? I'm open to ideas.
Thanks!
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我相信从你的描述中这就是你想要的。
我不确定的一件事是,如果文件存在于 dir1 而不是 dir2 中,您想要什么。
I believe this is what you want from your description.
One thing I wasn't sure about is what you wanted if a file exists in dir1 but not dir2.
线程
从那里复制的:
The thread yonder solves your problem quite nicely, I should think!
Copied from there:
您也可以不使用 bash 脚本来完成此操作:
diff -qr ./dir1 ./dir2 | sed -e 's/^仅在\(.*\): \(.*\)/\1\/\2/g' -e 's/ 和 \..*differ$//g' -e 's/^文件 //g' | xargs -I '{}' cp -Rf --parents '{}' ./dir3/
此解决方案使用 sed 从 diff 命令中删除所有附加文本,然后复制保留目录结构的文件。
You can also do it without a bash script:
diff -qr ./dir1 ./dir2 | sed -e 's/^Only in\(.*\): \(.*\)/\1\/\2/g' -e 's/ and \..*differ$//g' -e 's/^Files //g' | xargs -I '{}' cp -Rf --parents '{}' ./dir3/
This solution removes all additional text from the diff command using sed, and then copies the files preserving the directory structure.
之前发布的两个答案帮助我入门,但并没有让我一路走下去。 thomax 发布的解决方案非常接近,但我遇到了一个问题,osx 上的 cp 命令不支持 --parents 参数,因此我必须在创建子文件夹时添加一些逻辑,这使事情变得有点混乱,我有稍微重组一下。这就是我的结论:
The two previously posted answers helped me get started but didn't get me all the way there. The solution posted by thomax was really close but I ran into an issue where the cp command on osx doesn't support the --parents parameter so I had to add some logic around the creation of subfolders which made things a bit messy and I had to restructure a bit. Here's what I wound up with:
假设您的
dir1
、dir2
和dir3
位于同一级别内容设置如下:
当您像这样创建并应用补丁时:
然后您将拥有
dir2
和dir3
等效内容。如果您的
dir2
与dir1
不在同一级别然后你必须编辑补丁中的文件名
这样你就有相同数量的路径组件
在
dir1
和dir2
文件名中。您最好将
dir2
置于与dir1
相同的级别,因为没有优雅的方法可以做到这一点(至少我知道)。
这里遵循一个“丑陋”的方式。
考虑您的
dir2
位于某个$BASEDIR
中那么你应该更新你的 diff 以修剪
dir2
路径中的$BASEDIR
像这样
然后你可以应用上面生成的路径。
Consider you have
dir1
,dir2
anddir3
on the same levelwith the content setup as below:
When you create and apply patch like this:
Then you have content of
dir2
anddir3
equivalent.If your
dir2
is not at the same level asdir1
then you have to edit filenames in the patch
so that you have equal amount of path components
in both
dir1
anddir2
filenames.You should better put your
dir2
to the same level asdir1
,because there is no elegant way to do this (at least known to me).
Here follow an "ugly" way.
Consider your
dir2
is located in some$BASEDIR
then you should update your diff to trim of the
$BASEDIR
fromdir2
's pathlike this
And then you could apply the resulting path as above.