Comparing two files is a common task. On my MacBook I use the FileMerge (opendiff) app, which is part of the XCode tool-set. However, there are times when I am at the Linux command line and need to compare two files. In this blog post I will show how to compare files at the command line and with the emacs and vim editors.
To start, here are two simple python files that I will compare:
The differences between the two file are:
- The values of variable
assertstatements are different
- file1.py has an extra line at the end: it prints the value of z
Command line tools
diff command compares files line by line. It is invoked as follows:
diff file1.py file2.py
Let’s break down the output:
2c2 <-- The first '2' is line 2 in file1. 'c' means 'change'. The second 2, is line 2 in file2. < y = 3 <-- The '<' indicates the line is from file1. --- <-- formatting line, the different line(s) from file2 are below this > y = 7 <-- The '>' indicates the line is from file2 6,7c6 <-- The next difference. It is on line 6 & 7 of file1, and line 6 of file2. < assert z == 8 <-- line 6 from file1 < print z <-- line 7 from file1 --- > assert z == 12 <-- line 6 from file2. Note, there is no line 7 in file2
This is the “normal” output format of the
diff command without any options. There are many ways to configure the
colordiff command is a wrapper around the
diff command. It displays the same output as the diff command but with ‘syntax’ highlighting. You may need to install
colordiff on your machine. On Ubuntu or Debian you can do this using
apt-get install colordiff. Alternatively, you can download it from the http://www.colordiff.org/ website.
colordiff command is invoked as follows:
colordiff file1.py file2.py
The colour scheme used for highlighting can be changed. The file is located in
/etc/colordiffrc but override it in
~/.colordiffrc. Alternate colour themes are available at https://github.com/daveewart/colordiff
cmp command compares two files byte by byte. If the files are different then it reports the first byte and line number where they differ.
cmp file1.py file2.py
It will also report if one file is the prefix of another. What does it mean that one file is a prefix of another? It means the contents of one file are the starting contents of another file. For example:
IT WAS the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness,
IT WAS the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness,
In this example a.txt is a prefix of b.txt.
cmp a.txt b.txt
sdiff command does a side by side merge of file difference. When
sdiff is invoked without the –output (-o) option it produces a side-by-side difference. However, this usage is considered depricated.
diff -y file1 file2 should be used instead. This screen shot shows the output from using
diff -y file1 file2.
To compare two files in the emacs editor use the following command:
Note: M means meta key and is usually either the ALT or OPTION key. Example:
Emacs will then provide prompts to enter the file names. At the resulting differences screen it provides you with the option to ‘Type ? for help’. This lists the commands for moving around, changing display features and manipulating the files. But briefly, to navigate through the changes use:
DEL- move to the previous difference
SPACE- move to the next difference
The vim editor lets you compare files too. Use the following command to view the differences using a side-by-side comparison:
vimdiff file1.py file2.py
To view the differences using horizontal splits use the -o argument:
vimdiff -o file1.py file2.py
For either display option you can navigate the through the differences using these commands:
[c- Jump back to the previous start of a change. The c is optional and stands for count. When c is used jump back ‘c’ times
]c- Jump to the start of the next change
This was a very brief overview of how to compare files. Please see the references below for more details on each of the methods.