187

How can I plot the results of a unix diff command side-to-side instead of one difference after the other? See below for an example:

    diff /tmp/test1  /tmp/test2
1,4c1,2
< asfdsadf
< asdfsad
< fsaf
< fdsadf
---
> asdfsafdsf
> saf
6,8d3
< sadf
< asdf
< sadf
10d4
< fasd
12,13c6,14
< sadfa
< fd
---
> sadf
> sadf
> sadf
> sadf
> sadf
> sadf
> sadf
> sadf
> safa

I would like to have something like:

diff /tmp/test1  /tmp/test2
1,4c1,2
< asfdsadf       > asdfsafdsf
< asdfsad        > saf       
< fsaf
< fdsadf
---
6,8d3
< sadf
< asdf
< sadf
10d4
< fasd
12,13c6,14
< sadfa               > sadf
< fd              > sadf
---               > sadf
              > sadf
              > sadf
              > sadf
              > sadf
              > sadf
              > safa
3
  • vimdiff can help too. Good luck.
    – shellter
    Jun 28, 2013 at 16:05
  • sdiff -s f1 f2
    – Daniel
    May 24, 2021 at 21:03
  • I don't know why, but 'diff -y' and 'sdiff' don't seem to maintain the formatting when copy/pasting...however vimdiff does...(but remember... <esc>:q! to quit) Ah, as per below, diff needs '--expand-tabs' to use spaces instead of tabs :D Jan 25, 2022 at 7:39

11 Answers 11

269

From man diff, you can use -y to do side-by-side.

-y, --side-by-side
       output in two columns

Hence, say:

diff -y /tmp/test1  /tmp/test2

Test

$ cat a                $ cat b
hello                  hello
my name                my name
is me                  is you

Let's compare them:

$ diff -y a b
hello                                                           hello
my name                                                         my name
is me                                                         | is you
5
  • 51
    Note that diff has a hard-coded maximum output width (130 columns). Add the diff option --width=$COLUMNS to set this to your terminal width.
    – ntc2
    Nov 25, 2013 at 20:13
  • 8
    Also for large diffs just pipe into less like this for a nice scrolly/searchable diff: diff -y /tmp/test1 /tmp/test2 | less Feb 24, 2016 at 22:34
  • 1
    check out colordiff Aug 18, 2017 at 18:19
  • 21
    better diff --width=$COLUMNS --suppress-common-lines --side-by-side a b
    – rubo77
    Nov 27, 2019 at 4:07
  • 2
    @rubo77 Depending on how you have set up tab expansion, you may also want to expand tabs to spaces so that the output lines up properly in columns: diff --expand-tabs --width=$COLUMNS --suppress-common-lines --side-by-side a b Jun 26, 2020 at 17:05
98
diff -y --suppress-common-lines file1 file2
5
  • 13
    this should be the accepted answer because just -y shows common lines as well which is not same as just diff and the original question. +1.
    – helix
    Feb 8, 2019 at 10:18
  • 1
    better diff -W $COLUMNS --suppress-common-lines -y file1 file2
    – rubo77
    Nov 27, 2019 at 4:09
  • 1
    late to the party :-( . but what is -W $COLUMNS. Can someone eloborate
    – samshers
    Aug 26, 2020 at 14:06
  • 3
    @samshers -W defines the width, which is useful if you have a big terminal, and want to use it. $COLUMNS is a built-in which tells the width of your current terminal. Frankly, I don't see its benefit for my usage.
    – mazunki
    Sep 18, 2020 at 17:16
  • 3
    @mazunki, I find it useful when using a large terminal because long lines may get truncated with just -y --suppress-common-lines. I also add the --color modifier thus getting $ diff --color -y --suppress-common-lines -W $COLUMNS file1 file2.
    – Daniel
    May 24, 2021 at 20:50
66

From icdiff's homepage:

enter image description here

Your terminal can display color, but most diff tools don't make good use of it. By highlighting changes, icdiff can show you the differences between similar files without getting in the way. This is especially helpful for identifying and understanding small changes within existing lines.

Instead of trying to be a diff replacement for all circumstances, the goal of icdiff is to be a tool you can reach for to get a better picture of what changed when it's not immediately obvious from diff.

IMHO, its output is much more readable than diff -y.

25

You can use:

sdiff  file1 file2

or

diff -y file1 file2

or

vimdiff file1 file2

for side by side display.

1
  • 3
    I was wondering what's the difference between diff -y and sdiff? Their output looks identical to me.
    – Hux
    May 7, 2019 at 6:01
13

You should have sdiff for side-by-side merge of file differences. Take a read of man sdiff for the full story.

1
  • 3
    On my system (GNU/Linx) sdiff appears to do the same thing as diff -y and the sdiff info documentation says it's deprecated: "'sdiff' without '--output' ('-o') produces a side-by-side difference. This usage is obsolete; use the '--side-by-side' ('-y') option of 'diff' instead."
    – ntc2
    Nov 25, 2013 at 20:08
9

You can simply use:

diff -y fileA.txt fileB.txt | colordiff

It shows the output splitted in two colums and colorized! (colordiff)

3
9

If your files have inconsistent use of spaces and tabs, you may find it helpful to include the -t argument to expand the tabs:

diff -ty file1 file2
8

You can use vimdiff.

Example:

vimdiff file1 file2
7

Try cdiff - View colored, incremental diff in workspace or from stdin with side by side and auto pager support.

1
6

Use the -y option:

diff -y file1 file2
3

Enhanced diff command with color, side by side and alias

Let's say the file contents are like:

cat /tmp/test1.txt
1
2
3
4
5
8
9

and

cat /tmp/test2.txt
1
1.5
2
4
5
6
7

Now comparing side-by-side

diff --width=$COLUMNS --suppress-common-lines --side-by-side --color=always /tmp/test1.txt /tmp/test2.txt
                                                                              > 1.5
3                                                                             <
8                                                                             | 6
9                                                                             | 7

You can define alias to use

alias diff='diff --width=$COLUMNS --suppress-common-lines --side-by-side --color=always'

Then new diff result:

diff /tmp/test1.txt /tmp/test2.txt
                                                                              > 1.5
3                                                                             <
8                                                                             | 6
9                                                                             | 7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.