シェルスクリプトを使用して、2 つの異なるディレクトリにある同じ名前のファイルを比較する方法

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

  •  02-07-2019
  •  | 
  •  

質問

SVN の使用に移行する前は、単に /develop/ ディレクトリに保存し、そこでファイルを編集およびテストしてから、それらのファイルを /main/ ディレクトリ。SVN に移行することに決めたとき、ディレクトリが実際に同期していることを確認する必要がありました。

では、2 つの異なるディレクトリにある同じ名前のファイルを再帰的に比較するシェル スクリプト [ bash ] を記述する良い方法は何でしょうか?

注記:上記で使用されているディレクトリ名はサンプルのみです。コードをトップレベルに保存することはお勧めしません:)。

役に立ちましたか?

解決

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 では、再帰的な差分が可能です。

diff -r main develop

ただし、シェルスクリプトを使用すると、次のようになります。

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

[自分の質問に答えるのはOKだとどこかで読んだので、ここに書いておきます:)]

これを試してみましたが、かなりうまくいきました

[/]$ 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, 、次のようになります。

  • 2 つの再帰パスで、配列内のファイルとディレクトリを、それらが存在するディレクトリ (または両方) に応じて並べ替えます。
  • 両方のディレクトリにあるファイルは、次のいずれかに応じて 2 つの配列に再度並べ替えられます。 diff -q 異なるかどうかを判断する
  • それらのファイルについては、 diff クレームが等しい、タイムスタンプを表示して比較する

お役に立てば幸いです - 乾杯!

編集2:(実際、リモート ファイルでは問題なく動作します。問題は、ローカル ファイルとリモート ファイル間の差分操作中に 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 - ディレクトリ内を再帰します
  • -v - 冗長(しかし ない ファイル変更情報に関連)
  • --cvs-exclude - 無視する .svn ファイル
  • -i - "--itemize-changes:すべての更新の変更概要を出力します」

以下はその簡単な抜粋です 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, これは、 dir1 にはあるが dir2 は見つからない、または dir2 にはあるが dir1 は見つかっていないファイルを最初にリストするシェル スクリプトです (出力の最初のページ、 pr コマンドを実行し、見出しでページ分割されます)、続いて各共通ファイルと分析を比較します (同じ、異なる、ディレクトリが最も一般的な結果でした)。

これは消滅しつつあるようです。必要に応じて、独立した再実装を用意しています。それはロケット科学ではありません(cmp あなたの友だちです)。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top