Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

diffコマンドとpatchコマンドで差分を扱う

diffコマンドとpatchコマンドを使うことで、
差分を楽に扱うことができます。

次のようなファイル群があったとします。

$ ls
difffile.diff	test.txt	test_diff.txt

$ tail +1 *
==> difffile.diff <==
--- test.txt	2015-01-04 21:46:58.000000000 +0900
+++ test_diff.txt	2015-01-04 21:47:52.000000000 +0900
@@ -1,2 +1,2 @@
-This is a sample text.
+This is a sample post.
 

==> test.txt <==
This is a sample text.


==> test_diff.txt <==
This is a sample post.

ここで、patchコマンドを使うと…

$ patch test.txt < difffile.diff 
patching file test.txt

変更先のファイルはちゃんと差分が反映されていることがわかります。

$ tail +1 *
==> difffile.diff <==
--- test.txt	2015-01-04 21:46:58.000000000 +0900
+++ test_diff.txt	2015-01-04 21:47:52.000000000 +0900
@@ -1,2 +1,2 @@
-This is a sample text.
+This is a sample post.
 

==> test.txt <==
This is a sample post.


==> test_diff.txt <==
This is a sample post.

diffコマンドで差分をとっても…

$ diff test.txt test_diff.txt 
$

なにもでません。