Loose-Info.com
Last Update 2014/03/11
TOP - Unix系OS - Linux - diff(1)

ファイルを比較します。
出力形式指定の無い場合の通常書式は、最初のファイルにどのような変更を加えると2番目のファイルになるかを示したものです。

diff オプション1 ファイルパス1 ファイルパス2
オプション1(任意) 無しの場合 各差異部分の1行目の出力書式 値1 a 値2 (ファイルパス1)のファイルの(値1)の行の後に、 (ファイルパス2)のファイルの(値2)の範囲の行を追加 値1 c 値2 (ファイルパス1)のファイルの(値1)の範囲の行を、 (ファイルパス2)のファイルの(値2)の範囲の行で置換 値1 d 値2 (ファイルパス1)のファイルの(値1)の範囲の行を削除。 (ファイルパス2)のファイルでの位置は(値2)の行の後 -b 連続する空白の数を無視 -c コンテキストを使用した書式で出力 ◎ヘッダ部分の書式 *** (ファイルパス1)のファイル名 (ファイルパス1)のタイムスタンプ --- (ファイルパス2)のファイル名 (ファイルパス2)のタイムスタンプ ◎各差異部分の出力書式 *************** *** (ファイルパス1)の行範囲 **** 行詳細・・・ ・・・ --- (ファイルパス2)の行範囲 ---- 行詳細・・・ ・・・ ◎各差異詳細の出力書式 ! (ファイルパス1)と(ファイルパス2)で変更のある部分 + (ファイルパス2)に追加された部分 - (ファイルパス1)から削除された部分 -i 大文字小文字の差を無視 -q 差異の内容ではなく結論のみ出力 -u unified形式で出力 ◎ヘッダ部分の書式 --- (ファイルパス1)のファイル名 (ファイルパス1)のタイムスタンプ +++ (ファイルパス2)のファイル名 (ファイルパス2)のタイムスタンプ ◎各差異部分の出力書式 @@ (ファイルパス1)の行範囲 (ファイルパス2)の行範囲 @@ 行詳細・・・ ・・・ ◎各差異詳細の出力書式 + この位置で(ファイルパス1)に行の追加 - この位置で(ファイルパス1)から行の削除 -W 並べて表示の際に行の最大幅を指定 -w 空白を無視 -y (ファイルパス1)と(ファイルパス2)の行を並べて表示 中央部分に差異の内容を記号で表示 ◎差異の内容を示す記号(主要なもののみ記載) 空白 差異が無い、もしくは無視されている | 行の内容に差異がある < (ファイルパス1)のみに含まれている > (ファイルパス2)のみに含まれている ファイルパス1 比較するファイル(ディレクトリ)へのパス ファイルパス2 比較対象となるファイル(ディレクトリ)へのパス

※ オプションなどは個人的に重要と考えられるものを記述しており、記載の無いものは、manページや関連書籍などを参照願います。
※ 実行例の記述は、不要と考えられる部分の削除などの修正を行ったものを掲載しています。
※ 実行例の実行環境はUbuntu 12.10です。

(例) オプション無し(その1)
実行結果
$ cat test1.txt a $ cat test2.txt b $ diff test1.txt test2.txt 1c1 ← test1.txtの1行目をtest2.txtの1行目で置換 < a ← test1.txtの1行目 --- > b ← test2.txtの1行目

(例) オプション無し(その2)
実行結果
$ cat test1.txt a b $ cat test2.txt c d $ diff test1.txt test2.txt 1,2c1,2 ← test1.txtの1〜2行目をtest2.txtの1〜2行目で置換 < a ← test1.txtの1行目 < b ← test2.txtの2行目 --- > c ← test2.txtの1行目 > d ← test2.txtの2行目

(例) オプション無し(その3)
実行結果
$ cat test1.txt a b c $ cat test2.txt aa b cc $ diff test1.txt test2.txt 1c1 ← 1つ目の相違点 : test1.txtの1行目をtest2.txtの1行目で置換 < a ← test1.txtの1行目 --- > aa ← test2.txtの1行目 3c3 ← 2つ目の相違点 : test1.txtの3行目をtest2.txtの3行目で置換 < c --- > cc

(例) オプション無し(その4)
実行結果
$ cat test1.txt a b d c $ cat test2.txt a b c $ diff test1.txt test2.txt 3d2 ← test1.txtの3行目を削除。test2.txtでの位置は2行目の後 < d ← test1.txtの3行目

(例) オプション無し(その5)
実行結果
$ cat test1.txt a b c $ cat test2.txt a b d c $ diff test1.txt test2.txt 2a3 ← test1.txtの2行目の後にtest2.txtの3行目を追加 > d ← test2.txtの3行目

(例) オプション -b
実行結果
$ cat test1.txt a a $ cat test2.txt a a $ diff test1.txt test2.txt 1c1 < a a --- > a a $ diff -b test1.txt test2.txt ← -bオプションで結果は差異無し

(例) オプション -c
実行結果
$ cat test1.txt abc def 111 222 333 444 555 $ cat test2.txt abc defg 111 666 333 555 777 $ diff -c test1.txt test2.txt *** test1.txt Tue Feb 18 14:42:49 2014 ← ヘッダ部分 --- test2.txt Tue Feb 18 14:46:08 2014 *************** *** 1,7 **** ← (ファイルパス1)差異部分 1〜7行目 abc ! def ← 変更 111 ! 222 ← 変更 333 - 444 ← 削除 555 --- 1,7 ---- ← (ファイルパス2)差異部分 1~7行目 abc ! defg ← 変更 111 ! 666 ← 変更 333 555 + 777 ← 追加

(例)cオプション -i
実行結果
$ cat test1.txt a b c $ cat test2.txt a B ← 大文字 c $ diff test1.txt test2.txt 2c2 < b --- > B $ diff -i test1.txt test2.txt ← -iオプションで結果は差異無し

(例) オプション -q
実行結果
$ cat test1.txt a b $ cat test2.txt a c $ diff -q test1.txt test2.txt Files test1.txt and test2.txt differ ← 比較の結果のみ出力

(例) オプション -u
実行結果
$ cat test1.txt abc def 111 222 333 444 555 $ cat test2.txt abc defg 111 666 333 555 777 $ diff -u test1.txt test2.txt --- test1.txt 2014-02-18 14:42:49.670440153 +0900 +++ test2.txt 2014-02-18 14:46:08.631424375 +0900 @@ -1,7 +1,7 @@ abc -def +defg 111 -222 +666 333 -444 555 +777

(例)オプション -w
実行結果
$ cat test1.txt a a bb $ cat test2.txt a a b b $ diff -b test1.txt test2.txt 2c2 < bb --- > b b $ diff -w test1.txt test2.txt ← -wオプションで結果は差異無し

(例)オプション -y
実行結果
$ cat test1.txt abc def 111 222 333 444 555 $ cat test2.txt abc defg 111 666 333 555 777 $ diff -y -W 50 test1.txt test2.txt abc abc def | defg 111 111 222 | 666 333 333 444 < 555 555 > 777