如何比较文件具有相同名称的两个不同的目录中使用一个脚本

StackOverflow https://stackoverflow.com/questions/119788

  •  02-07-2019
  •  | 
  •  

在行动之前使用SVN,我用来管理我的项目,通过简单的保留 /develop/ 目录和编辑和测试文件,然后将它们移动到 /main/ 目录。当我决定搬到SVN,我需要可以肯定的是,的目录,实际上是同步的。

那么,什么是一个很好的方式编写脚本[砸]递归的比较文件中具有相同名称的两个不同的目录?

注:该目录的名称使用上述样品只。我不推荐储存的代码顶级:).

有帮助吗?

解决方案

diff命令有一个-r选项来递归比较目录:

diff -r /develop /main

其他提示

diff -rqu /develop /main

它只会给你一个改变的总结:)

如果您只想查看新/缺少的文件

diff -rqu /develop /main | grep "^Only

如果你想让他们裸露:

diff -rqu /develop /main | sed -rn "/^Only/s/^Only in (.+?): /\1/p"

我可用的差异允许递归差异:

diff -r main develop

但是使用shell脚本:

( cd main ; find . -type f -exec diff {} ../develop/{} ';' )

[我在某个地方读到了回答你自己的问题是好的,所以这里有:)]

我试过这个,而且效果很好

[/]$ cd /develop/
[/develop/]$ find | while read line; do diff -ruN "/main/$line" $line; done |less

您可以选择仅比较特定文件[例如,仅编译.php文件],将上述行编辑为

[/]$ cd /develop/
[/develop/]$ find -name "*.php" | while read line; do diff -ruN "/main/$line" $line; done |less

还有其他想法吗?

这里是一个例子(有些凌乱)脚本我的 dircompare.sh, ,它将:

  • 排序文件和目录在阵列,根据目录中它们发生在(或两者),在两个通过递归
  • 该文件中出现两个目录,都是根据再次在两个阵列,这取决于如果 diff -q 确定如果它们不同或不
  • 对于那些文件 diff 索赔都是平等的,显示出比较的时间戳

希望可以找到有用的-干杯!

EDIT2:(实际上,它的工作的现有的远程文件的问题是未经处理的Ctrl-C信号在一个差异作之间的当地和远程的文件,这可能需要一段时间;脚本现在更新一个陷阱来处理,但是,在离开以前的编辑下面的参考):

编辑:...除了它似乎要崩溃了我的服务器,用于远程ssh目录(这是我试图使用过 ~/.gvfs)...所以这不是 bash 了,但一个替代我的猜测是使用 rsync, ,这里是一个例子:

$ # get example revision 4527 as testdir1
$ svn co https://openbabel.svn.sf.net/svnroot/openbabel/openbabel/trunk/data@4527 testdir1

$ # get earlier example revision 2729 as testdir2
$ svn co https://openbabel.svn.sf.net/svnroot/openbabel/openbabel/trunk/data@2729 testdir2

$ # use rsync to generate a list 
$ rsync -ivr --times --cvs-exclude --dry-run testdir1/ testdir2/
sending incremental file list
.d..t...... ./
>f.st...... CMakeLists.txt
>f.st...... MACCS.txt
>f..t...... SMARTS_InteLigand.txt
...
>f.st...... atomtyp.txt
>f+++++++++ babel_povray3.inc
>f.st...... bin2hex.pl
>f.st...... bondtyp.h
>f..t...... bondtyp.txt
...

注意:

  • 得到上述的,你绝不能忘记后斜线 / 在结束目录名 rsync
  • --dry-run -模拟只不更新/文件传输
  • -r -recurse入目录
  • -v -详细的(但是 有关文件的更改信息)
  • --cvs-exclude -忽略 .svn 文件
  • -i -"--详细列举-改变:输出变化摘要中的所有更新"

这是一个简要的摘录 man rsync 这说明显示的信息 -i (例如,《 >f.st...... 串以上):

The  "%i"  escape  has a cryptic output that is 11 letters long.
The general format is like the string YXcstpoguax,  where  Y  is
replaced  by the type of update being done, X is replaced by the
file-type, and the other letters represent attributes  that  may
be output if they are being modified.

The update types that replace the Y are as follows:

o      A  < means that a file is being transferred to the remote
       host (sent).

o      A > means that a file is being transferred to  the  local
       host (received).

o      A  c  means that a local change/creation is occurring for
       the item (such as the creation  of  a  directory  or  the
       changing of a symlink, etc.).

...
The file-types that replace the X are: f for a file, a d  for  a
directory,  an  L for a symlink, a D for a device, and a S for a
special file (e.g. named sockets and fifos).

The other letters in the string above  are  the  actual  letters
that  will be output if the associated attribute for the item is
being updated or a "." for no change.  Three exceptions to  this
are:  (1)  a newly created item replaces each letter with a "+",
(2) an identical item replaces the dots with spaces, and (3)  an
....

一位神秘的确-但至少它显示了基本目录作比较 ssh.干杯!

经典(System V Unix)答案是 dircmp dir1 dir2 ,这是一个shell脚本,它将列出在dir1但不是dir2或dir2但在开头没有dir1的文件中找到的文件(第一页输出,来自 pr 命令,所以用标题分页),然后用分析比较每个公共文件(相同,不同,目录是最常见的结果)。

这似乎是在消失的过程中 - 如果你需要,我可以独立重新实现它。这不是火箭科学( cmp 是你的朋友)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top