Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have tried using the linux diff command in the past without much luck or patience. I usually end up using a GUI utility like DiffMerge or Kdiff.

Recently I started trying to use diff again to compare files on remote systems via ssh, and I have found the -y / --side-by-side option to be very useful, but I still feel like I'm not making this most of this utility.

So I'm curious of how to achieve the following things:

  1. Highlight changes or use color-coding to make the output more readable
  2. Merge - Select lines with differences and effect the changes into one of the files

Also, I haven't found many good examples when googling, so if you have some linux-fu secrets for getting the most out of diff, could you please share? I will mainly be comparing text-based configuration files but any tricks and tips would be wonderful.

For Reference - The DiffMerge utility easily shows changes side by side with color highlighting. Icons at the top allow you to switch between views of "show all," "show differences," and "show differences with context." Would love some of this functionality at the command prompt.

Picture.png

ps: I should also note that one other option that I find quite useful is --suppress-common-lines which I learned about on this concise and readable blog post.

share|improve this question
3  
I use vimdiff quite a bit. –  nicerobot Nov 23 '11 at 17:00
    
@nicerobot You could post this as an answer - it is a very useful and helpful tool. –  rozcietrzewiacz Nov 23 '11 at 17:12
    
You cant, Its Free..... Sorry Couldn't help it :D –  whoami Nov 23 '11 at 17:26
    
I do most of my diffing in Emacs. –  Gilles Nov 23 '11 at 20:47
    
@whoami - what? –  cwd Nov 24 '11 at 0:11
add comment

4 Answers 4

up vote 7 down vote accepted

In no particular sequence:

  • meld is a very nice diff program which does very nice diffs and three-way merges.
  • git config --global merge.conflictstyle diff3 gets you three-way merge output for use with tools like meld.
  • wdiff does word diffs, very nice if colored: wdiff -w "$(tput bold;tput setaf 1)" -x "$(tput sgr0)" -y "$(tput bold;tput setaf 2)" -z "$(tput sgr0)" ...
  • To minimize cruft in the diff output, I usually use the --ignore-all-space (-w) option
  • diff-ignore-moved-lines* does what it says on the label.
  • difff* can be used to diff only lines which match in given fields.

* Disclaimer: I'm the author, and developed these to help with CLI and GUI diffing and merging.

share|improve this answer
1  
I also use diffuse quite a bit. –  Arcege Nov 24 '11 at 3:18
    
diff -u is also fairly straightforward, I find. –  gabe. Nov 24 '11 at 18:59
add comment

I've found vimdiff to be very useful.

share|improve this answer
add comment

Try sdiff

diff also has options (-e or -ed) to create a merge script for use with ed

sdiff, diff and ed should be part of your shell where ever you go.

Your version control software probably also has a diff and merge tool built in.

share|improve this answer
add comment

To get coloring in the diff you should look at http://colordiff.sourceforge.net/ . This is just a wrapper around diff and, as such, all the command options still work.

In case you have ubuntu just write:

    # sudo apt-get install colordiff

Running the risk of not meeting your use case, git is a very nice VCS that integrates well with many diffing and merging tools (both command line and GUI ones). Give it a try if it is an option.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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