Linux Diff Command

How Do I Compare Two Files and Show The Differences?

If, when you use find , you discover two files with suspiciously similar names, you might want to know whether they really contain the same information. The diff command will compare two files and give you the lowdown on just how different they are. Here's an example of how you would use the diff command and the output you might get from it: diff cookies.old
< One cup vanilla extract
> One teaspoon vanilla extract
< Six ounces chopped liver
> Note: The previous version of this recipe had a few errors!

The output is actually a description of how to transform the old file into the new one. Here, diff is telling you three things:

The fifth line of the file has undergone a change. The 5c5 message says to replace line 5 of the old file with line 5 of the new file. Both the old and new text are displayed, separated by a line of three dashes. (The less-than (<) notation means "remove this line," and the greater-than (>) sign means "add this line.")

Line 7 of the old file does not appear in the new file. The 7d6 message says to delete line 7 from the old file, and the files will then be in sync, starting at line 6 of the new file. The text to be deleted is displayed on the next line.

A line was added to the new file. The 21a22 message says to add a new line after line 21 of the old file. The text to be added is displayed on the final line of the output.

Two useful flags you can specify when comparing files are -b (ignore blanks) and -i (ignore case). You can use them separately or in combination. The ignore blanks feature is especially useful when you're comparing the source code for two programs, since indentation changes are rarely significant. For example, here's how you might compare two program files, ur2cool.c and ur2cool.backup:

diff -b -i ur2cool.c ur2cool.backup

Don't worry if you have some difficulty understanding the output of the diff command. It's c ryptic because it was designed to be used by computers, not humans. (Some source-code control systems used by programmers use diff to keep a record of the changes made to a program over time.)

Do pay attention, though, to the less-than and greater-than indicators to see at a glance what was added or deleted; and be happy if you manage to get a general feeling for the ways the files differ.

For more information on the diff command, see the diff manual.

Previous Lesson: Searching for Files
Next Lesson: Task Scheduling



It seems that comments script is not properly installed. See readme.txt for more info.

Comments - most recent first
(Please feel free to answer questions posted by others!)

No comments yet

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
Notify me about new comments on this page
Hide my email

Ask Bob Rankin - Free Tech Support

Copyright by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.