1つのディレクトリに多くのファイルがあり、ファイルがすべて一意であることを確認したいと思います。単純化のためにfoo.txt
、bar.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}'