Table of Contents

Linux Commands Cheat Sheet

diff : How to use diff command in Linux with examples



man diff

$ man diff
       -u     Use the unified output format.

       --side-by-side
              Use the side by side output format.

       --suppress-common-lines
              Do not print common lines in side by side format.

       -y     Use the side by side output format.

       -W columns
       --width=columns
              Use an output width of columns in side by side format.


Example Command

diff /etc/test{,.`date '+%Y%m%d'`}


Example

$ cat a.txt
1
2
3
4
5
$ cat b.txt
1
2
3a
4
5
$
$ diff  a.txt b.txt
3c3
< 3
---
> 3a
$
$ diff -u a.txt b.txt
--- a.txt       2016-01-14 02:05:34.000000000 +0000
+++ b.txt       2016-01-14 02:04:23.000000000 +0000
@@ -1,5 +1,5 @@
 1
 2
-3
+3a
 4
 5
$
$ diff -y a.txt  b.txt
1                                                               1
2                                                               2
3                                                             | 3a
4                                                               4
5                                                               5
$
$ diff --suppress-common-lines --side-by-side a.txt b.txt
3                                                             | 3a
$


Tips

diff with local and remotehost

$ ssh x.x.x.x cat /etc/hosts | diff /etc/hosts  - 
$ diff <(ssh x.x.x.x cat /etc/hosts) /etc/hosts  


diff with remotehost and remotehost

$ diff <(ssh x.x.x.1 cat /etc/hosts) <(ssh x.x.x.2 cat /etc/hosts)
$ diff <(ssh x.x.x.1 "php -r \"phpinfo();\"") <(ssh x.x.x.2 "php -r \"phpinfo();\"")