「diff -q」を使用すると、反対の結果が得られます。コンテンツを印刷せずに同じファイルを一致させます。

「diff -q」を使用すると、反対の結果が得られます。コンテンツを印刷せずに同じファイルを一致させます。

1つのディレクトリに多くのファイルがあり、ファイルがすべて一意であることを確認したいと思います。単純化のためにfoo.txtbar.txtおよび3つのファイルがあるとしましょうbaz.txt。このループを実行すると、互いに比較して確認します。

$ for f in ./*; do for i in ./*; do diff -q "$f" "$i"; done; done
Files bar.txt and baz.txt differ
Files bar.txt and foo.txt differ
Files baz.txt and bar.txt differ
Files baz.txt and foo.txt differ
Files foo.txt and bar.txt differ
Files foo.txt and baz.txt differ

処理しようとする何百ものファイルが読み取れなくなります。ファイルを一覧表示する方が良いです。するこれにより、リストをすばやく調べて、ファイルが自分とのみ一致することを確認できます。マンページを見ると、この-sオプションがうまくいくと思いました。

$ for f in ./*; do for i in ./*; do diff -s "$f" "$i"; done; done
Files bar.txt and bar.txt are identical
Files baz.txt and baz.txt are identical
Files foo.txt and foo.txt are identical

...しかし実際には返品他のファイルの内容全体を印刷します。上記の動作のみが発生するようにこの動作を抑制する方法はありますか?

それとも、そこにありますか?これを行うことができる他のツール

ベストアンサー1

これにより、トリックを実行できます。

diff -rs dir1 dir2 | egrep '^Files .+ and .+ are identical$'

2つのディレクトリはどこにdir1あり、どこにありますか?dir2

一致するディレクトリのみを印刷したい場合dir1

diff -rs dir1 dir2 | egrep '^Files .+ and .+ are identical$' | awk -F '(Files | and | are identical)' '{print $2}'

同様に、一致するディレクトリだけを印刷したい場合dir2

diff -rs dir1 dir2 | egrep '^Files .+ and .+ are identical$' | awk -F '(Files | and | are identical)' '{print $3}'

おすすめ記事