LoginSignup
0
0
お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

diffコマンドのオプションと実行例をまとめてみた

Last updated at Posted at 2024-06-20

背景

diffコマンドのオプション、意外と知らんな…。
調べてみよう。

オプション一覧

オプション 説明
-c context形式で差分を表示
-u unified形式で差分を表示
-y
--side-by-side
横並びで差分を表示
-q
--brief
差分があれば、差分がある旨メッセージを出力
※差分は出ない
※差分がなければ出力もなし
-s 3差分があれば、差分を出力
無ければ、差分がない旨メッセージを出力
-b 空白差分は無視
-i
--ignore-case
大文字小文字の区別をしない
--suppress-common-lines 共通行を表示しない
※-yオプションと併用で効果あり
-W
--width=${NUM}
表示幅を指定する
※-yオプションと併用で効果あり
(指定なし:130カラム)
--color 色付きで差分表示する

ファイルの準備

text01.txt
$ cat text01.txt
aaa
bbb
ccc
ddd
eee
fff
text02.txt
$ cat text02.txt
aa11a
bbb
ccc222
ddd
eee333
fff

-c

$ diff -c text01.txt text02.txt
*** text01.txt  Wed Jun 19 14:35:42 2024
--- text02.txt  Wed Jun 19 14:36:04 2024
***************
*** 1,6 ****
! aaa
  bbb
! ccc
  ddd
! eee
  fff
--- 1,6 ----
! aa11a
  bbb
! ccc222
  ddd
! eee333
  fff

-u

$ diff -u text01.txt text02.txt
--- text01.txt  2024-06-19 14:35:42
+++ text02.txt  2024-06-19 14:36:04
@@ -1,6 +1,6 @@
-aaa
+aa11a
 bbb
-ccc
+ccc222
 ddd
-eee
+eee333
 fff

-y (もしくは--side-by-side)

$ diff -y text01.txt text02.txt
$ diff --side-by-side text01.txt text02.txt

aaa                             |       aa11a
bbb                                 bbb
ccc                             |       ccc222
ddd                                 ddd
eee                             |       eee333
fff                                 fff

-q (もしくは-brief)

$ diff -q text01.txt text02.txt

Files text01.txt and text02.txt differ

-s

差分あり.
$ diff -s text01.txt text02.txt

1c1
< aaa
---
> aa11a
3c3
< ccc
---
> ccc222
5c5
< eee
---
> eee333
差分なし.
$ diff -s text01.txt text01_copy.txt

Files text01.txt and text01_copy.txt are identical

-b

$ diff -b text01.txt text02.txt

1c1
< aaa
---
> aa11a
3c3
< ccc
---
> ccc222
5c5
< eee
---
> eee333

-i (もしくは--ignore-case)

text01.txtをベースに以下を作成(1, 3, 5行目だけ大文字にした)

text01_uppercase.txt
AAA
bbb
CCC
ddd
EEE
fff
$ diff -i text01.txt text01_uppercase.txt
# (差分はないので結果なし)
# オプションがないと差分が出ます

--suppress-common-lines

$ diff --suppress-common-lines text01.txt text02.txt

1c1
< aaa
---
> aa11a
3c3
< ccc
---
> ccc222
5c5
< eee
---
> eee333

-W (もしくは--width=${NUM})

$ diff --side-by-side -W 100 text01_long.txt text02_long.txt

スクリーンショット 2024-06-19 18.10.02.png

$ diff --side-by-side -W 50 text01_long.txt text02_long.txt

スクリーンショット 2024-06-19 18.10.42.png

$ diff --side-by-side -W 25 text01_long.txt text02_long.txt

スクリーンショット 2024-06-19 18.11.16.png

--color

$ diff --color  text01.txt text02.txt

添付画像を参照


こんな感じです

参考記事

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0