広告 Linux Mac

diffコマンドを便利に使う

configファイル修正したり、ソースをいじった際によくdiffコマンドを使って変更点を確認しますよね。

オプション無しで実行すると見にくいけど、あるオプションを付けてやると

gitっぽく出力してくれたり、WinMergeっぽく出力してくれるようになります。

覚えておくと便利なので、ぜひ見ていってください。

オプション無し

オプション無しでdiff を叩くと以下のように出力されます。

見にくい・・

慣れない人が見るとなんじゃこりゃとなりそうですね。

[root@bacchi ~]# diff /etc/ntp.conf.orig /etc/ntp.conf
8,9c8,10
< restrict default kod nomodify notrap nopeer noquery
< restrict -6 default kod nomodify notrap nopeer noquery
---
> restrict default ignore
> restrict -6 default ignore
> restrict ntp1.sakura.ad.jp kod nomodify notrap nopeer noquery
22,24c23
< server 0.centos.pool.ntp.org
< server 1.centos.pool.ntp.org
< server 2.centos.pool.ntp.org
---
> server ntp1.sakura.ad.jp

-u オプション

-u オプションをつけると見やすくなります。

エンジニアな人なら見慣れた形式ですが、エンジニアじゃない人にとってはまだとっつきにくいかな。

[root@bacchi ~]# diff -u /etc/ntp.conf.orig /etc/ntp.conf
--- /etc/ntp.conf.orig  2009-07-22 14:37:01.000000000 +0900
+++ /etc/ntp.conf       2012-09-17 20:55:27.571717816 +0900
@@ -5,8 +5,9 @@

 # Permit time synchronization with our time source, but do not
 # permit the source to query or modify the service on this system.
-restrict default kod nomodify notrap nopeer noquery
-restrict -6 default kod nomodify notrap nopeer noquery
+restrict default ignore
+restrict -6 default ignore
+restrict ntp1.sakura.ad.jp kod nomodify notrap nopeer noquery

 # Permit all access over the loopback interface.  This could
 # be tightened as well, but to do so would effect some of
@@ -19,9 +20,7 @@

 # Use public servers from the pool.ntp.org project.
 # Please consider joining the pool (http://www.pool.ntp.org/join.html).
-server 0.centos.pool.ntp.org
-server 1.centos.pool.ntp.org
-server 2.centos.pool.ntp.org
+server ntp1.sakura.ad.jp

 #broadcast 192.168.1.255 autokey       # broadcast server
 #broadcastclient                       # broadcast client

-ybBw オプション

一番のオススメの-ybBw オプション。

ファイルを左右に出力して、変更を|、追加削除を<>で表します。

WinMergeっぽく出力してくれるのでわかりやすい!

難点は小さい画面だととたんにダメな子になること。

[root@bacchi ~]# diff -ybBw /etc/ntp.conf.orig /etc/ntp.conf
# For more information about this file, see the man pages       # For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_mis   # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_mis

driftfile /var/lib/ntp/drift                                    driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do no   # Permit time synchronization with our time source, but do no
# permit the source to query or modify the service on this sy   # permit the source to query or modify the service on this sy
restrict default kod nomodify notrap nopeer noquery           | restrict default ignore
restrict -6 default kod nomodify notrap nopeer noquery        | restrict -6 default ignore
                                                              > restrict ntp1.sakura.ad.jp kod nomodify notrap nopeer noquery

# Permit all access over the loopback interface.  This could    # Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of       # be tightened as well, but to do so would effect some of
# the administrative functions.                                 # the administrative functions.
restrict 127.0.0.1                                              restrict 127.0.0.1
restrict -6 ::1                                                 restrict -6 ::1

# Hosts on local network are less restricted.                   # Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap        #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.             # Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/j   # Please consider joining the pool (http://www.pool.ntp.org/j
server 0.centos.pool.ntp.org                                  | server ntp1.sakura.ad.jp
server 1.centos.pool.ntp.org                                  <
server 2.centos.pool.ntp.org                                  <

Sponsor Link

-Linux, Mac