Comparing Text Files

If you want to compare the differences between two text files, one way to do this is to use the diff command. This is designed primarily to uncover small changes in otherwise identical documents, such as revisions made by another person. Of course, it can also be used to prove that two files are identical. If you run the files through diff, and it shows no output, it has been unable to spot any differences. diff is ordinarily used like this:

diff mytextfile1 mytextfile2 

If diff spots any differences between the files, the results are a little more complicated than you might be used to. Any lines that are different within the files will appear on screen. Those lines that are identical won't be displayed. Lines preceded with a left angle bracket (<) are from the first file, while those with a right angle bracket (>) are from the second file.

For a different display, you could type something like this:

diff –y mytextfile1 mytextfile2

This places the two lists side-by-side and highlights lines that are different with a pipe symbol (|). However, it requires a lot more screen space than using diff without the -y option.


When you use the -y command option with diff, it will struggle to fit the output in a standard Konsole window. If it is maximized on a 17-inch screen (1024×768 resolution), it should be just large enough to fit the information in, depending on the complexity of the files being compared.

By specifying the -a command option, you can make diff process binary files, too. This is a handy way of comparing virtually any kind of files, including program files, to see if they're identical. If there's no output from diff, then the two files are identical. If your screen fills with gibberish, then the files are clearly different.

Incidentally, if you want to compare three documents, you can use a very similar command: diff3. Check the command's man page to learn more about how it works.

Beginning SUSE Linux from Novice to Professional
Beginning SUSE Linux: From Novice to Professional
ISBN: 1590594584
EAN: 2147483647
Year: 2005
Pages: 293
Authors: Keir Thomas

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: