RHEL9 diffコマンドでファイルの差分を確認するサンプル

環境
# cat /etc/redhat-release
Red Hat Enterprise Linux release 9.2 (Plow)

操作方法
1.ファイルの内容を確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# cat test1.txt
111
222
333
# cat test1.txt 111 222 333
# cat test1.txt
111
222
333

# cat test2.txt

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
111
222
333
abc
def
111 222 333 abc def
111
222
333
abc
def

2.diffコマンドで差分を確認する
diff ファイル1 ファイル2
# diff test1.txt test2.txt
3a4,5
> abc
> def

3.-yオプションでファイルを左右に表示する
書式
diff -y ファイル1 ファイル2
実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# diff -y test1.txt test2.txt
111 111
222 222
333 333
> abc
> def
# diff -y test1.txt test2.txt 111 111 222 222 333 333 > abc > def
# diff -y test1.txt test2.txt
111                                                             111
222                                                             222
333                                                             333
                                                              > abc
                                                              > def

3.-cオプションでdiffコマンドで差分を確認する
書式
diff -c ファイル1 ファイル2
-cオプションをつけると以下のように出力されます。(Context形式)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# diff -c test1.txt test2.txt
*** test1.txt 2024-03-07 19:52:04.544359355 +0900
--- test2.txt 2024-03-07 19:52:21.532132069 +0900
***************
*** 1,3 ****
--- 1,5 ----
111
222
333
+ abc
+ def
# diff -c test1.txt test2.txt *** test1.txt 2024-03-07 19:52:04.544359355 +0900 --- test2.txt 2024-03-07 19:52:21.532132069 +0900 *************** *** 1,3 **** --- 1,5 ---- 111 222 333 + abc + def
#  diff -c test1.txt test2.txt
*** test1.txt   2024-03-07 19:52:04.544359355 +0900
--- test2.txt   2024-03-07 19:52:21.532132069 +0900
***************
*** 1,3 ****
--- 1,5 ----
  111
  222
  333
+ abc
+ def

4.diffコマンドで差分を確認する(-uオプション)
書式
diff -u ファイル1 ファイル2
-uオプションをつけると以下のように出力されます。(Unified形式)

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# diff -u test1.txt test2.txt
--- test1.txt 2024-03-07 19:52:04.544359355 +0900
+++ test2.txt 2024-03-07 19:52:21.532132069 +0900
@@ -1,3 +1,5 @@
111
222
333
+abc
+def
# diff -u test1.txt test2.txt --- test1.txt 2024-03-07 19:52:04.544359355 +0900 +++ test2.txt 2024-03-07 19:52:21.532132069 +0900 @@ -1,3 +1,5 @@ 111 222 333 +abc +def
# diff -u test1.txt test2.txt
--- test1.txt   2024-03-07 19:52:04.544359355 +0900
+++ test2.txt   2024-03-07 19:52:21.532132069 +0900
@@ -1,3 +1,5 @@
 111
 222
 333
+abc
+def						

 

IT

Posted by arkgame