diff and File Merge


Needing to check the difference between two text files today, I immediately thought of the diff command in terminal, though I’ve never used it before. Being text based, no doubt it’s not only more technical as well as more complex to wrap your head around.

But for the poor man (or the layman), there’s the File Merge application on the Mac. When I was trying to locate it today, I was looking for a program called ‘Difference’ or something, not ‘File Merge’. Merging these files is not really the endpoint I’m looking for when comparing files, though I guess it’s one of many possible reasons to use comparison er, software.

Enough ramble, it’s easy enough to use File Merge. Simply select the two text files you want to compare, and click ‘Compare’ in the bottom right hand corner, and what you get is a lovely visual view of whatever was changed in the file, from insertions, deletions and other changes. You beaut.

It’s a bit different with diff though I found, once I’d sent my boss along his merry way after answer his question in regards to the file comparison. The syntax required for diff is like so:

$ diff file1.txt file2.txt

Which generates output in the following format:

  1. Line numbers corresponding to the first file changes (2,4 means ‘lines 2 to 4’), with a letter indicating addition (a), change ©, or deletion (d), and finally line numbers corresponding to the second file changes.
  2. Lines changed in the first file.
  3. Separator.
  4. Lines changed in the second file.

If there’s an addition, it will only print the addition in the second file, and nothing from the first (as nothing is changed, technically?). The opposite is true for a deletion!

This is apparently default output though, and although I don’t have any examples to post because it’s company specific code, understandably it can be difficult to identify where these changes are given that there’s no highlighting. But in the GNU version there’s some other options, such as using the -c flag or the -u flag.

I haven’t got any further in testing or trying this so I’m going to link to a site I referenced from for more information, and hopefully I’ll get a chance to put this into use in the future. Perhaps with File Merge I’ll never need it, but you never know when it will come in use…