Site Menu
LINUX CLASSES - LINUX COMMANDS

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 cookies.new
5c5
< One cup vanilla extract
---
> One teaspoon vanilla extract
7d6
< Six ounces chopped liver
21a22
> 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

[ RETURN TO INDEX ]


   

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

Jarmo Visakorpi vpbn     (10 Apr 2013, 22:18)
|
Abdurrahman     (24 Apr 2012, 03:17)
Thanks!
carpinteyrotqu     (27 Mar 2012, 03:48)
All that you do, do with your might; things done by halves are never done right.
RobinHahl32     (02 Dec 2011, 22:35)
My friends and I thought this was super cool...good work!!
Steve     (08 Nov 2011, 13:18)
Thanks Dr Bob.

Saved me a lot of trouble.
John     (17 Oct 2011, 01:59)
Hi,

I found this website very useful, need to learn more commands.

John
Nitin     (11 Oct 2011, 16:25)
how do i store the result in new file...??
nitin     (11 Oct 2011, 16:24)
how do i store the difference in new file.
lakshmi     (20 Sep 2011, 03:57)
how to remove enter in od command
Troy     (10 Aug 2011, 22:04)
UPDATE:

I found what to do, I just use the program Meld
Dordoxito     (07 Aug 2011, 03:45)
Si, probablemente lo sea
Troy     (11 Jun 2011, 15:45)
How do you compare 2 txt files not using diff? diff compares line by line, my lines don't match up but I still need to know the differences.

Like seeing if the same words of sentence appear in 2 different txt files but their not on the same line.

Thanks
Nightrain     (02 Jun 2011, 08:08)
Thanks man u rock bob
Ven     (05 Apr 2011, 01:02)
Hi: I need an example for this to write a script that tells you whether the permissions for two files, whose names are given as arguments to the script, are identical. If the permissions for the two files are identical, output the common permission field. Otherwise, output each filename followed by its permission field. I know that ls -la | cut -d " " -f*
But didn't know how to write as a script
Bob Rankin     (15 Mar 2011, 20:13)
@Lucky - Did someone tell you that you could post homework assignments here?
lucky     (15 Mar 2011, 17:22)
write a shell script which receive two files as a argument. it should check whether the two file's content are same or not if they same then second file should be deleted(in linux)please give me programme
JR     (08 Jan 2011, 14:21)
Can I open a 'b.docs file with Fedora 9 Linux?
Editukx     (08 Jan 2011, 11:30)
can yuo tell me then i did: diff -s 1.txt 2.txt and i get 1,4c1,5 wat that value means ? because I can not find an answer :)

Louis     (23 Nov 2010, 10:48)
Offline conversation brought online, thank you Anes:

Thank you for the response. It works like a charm. I didn't even think about combining 1 and 3.

----------

pls try this option : Anyway, I would use 'comm' for this, see 'man comm', you'll probably use 'comm -13' or '-23'. This is the Reply of 1 of my Friend, Pls try this method and inform me your Result...

------------

Is there a way to use the diff command between an older and a newer version of a file and only display the lines that have been added to the newer file and not the ones that have been removed without any of the explanation formatting, just the new lines. I'm trying to bypass the process of putting both files into a database and running an SQL "left join where old data is null" as I will need to do this on a regular basis.
aa     (02 Nov 2010, 05:52)
aa
Surendra     (29 Oct 2010, 05:53)
very easy understandable explanation Bob..!!!
Thanks.
Rob     (01 Oct 2010, 21:19)
Hi seawaves/Suneel -
It sounds like you have two files (let's call them A.txt and B.txt), with 169 and 163 lines in each respectively, and that diff thinks they are *completely* different - thus, it's saying to change lines 1 through 169 in A.txt into lines 1 through 163 in B.txt. You could try the "-d" option, to use an algorithm "to perhaps find a smaller set of changes" (from the man page). If you look at the files and they seem similar (try "head A.txt B.txt" to see the first 10 lines of each), but diff always thinks they're different, you might have a problem with line endings (especially if you use both Linux/Unix and Windows). In that case, you can convert the line endings from Windows (DOS) to Linux (Unix) with "dos2unix -n A.txt A-new.txt" and "dos2unix -n B.txt B-new.txt", then diff A-new.txt and B-new.txt. (You can convert Unix line endings to Windows by using the command "unix2dos".) Hope that helps! (And thanks for your great explanation, Dr. Bob!)
Suneel     (01 Oct 2010, 01:17)
I got this when I ran diff command on two files

1,169c1,163
can someone please explain me this output...
seawaves     (15 Sep 2010, 12:30)
I got this when I ran diff command on two files

1,169c1,163

Can u please explain, how many lines and from which line number need to be copied from old file to new file and from which line upto which line in new file
linux beginner     (06 Aug 2010, 04:43)
Can I use diff or cmp command to compare two pdf files or doc files?
Anes     (05 Jul 2010, 09:31)
Hi Akshada ,

The difference b/w cmp and diff is:
The cmp command is a simpler version of diff, above.

Whereas diff reports the differences between two files, cmp merely

shows at what point they differ.

if you need any clarification , contact me
at anes.pa at gmail.com

bye anes
larry     (23 Jun 2010, 14:33)
Thanks for explaining a tough to understand topic in a very simple and easy to understand manner.
Akshada     (17 Jun 2010, 11:22)
Can we use "cmp" command instead of "diff" ?
What is the difference between both ?
vaidyanathan     (07 Jun 2010, 02:49)
Thanks A lot Bob... one of the few newser-friendly pages on the net..:)
Kevin     (27 May 2010, 04:00)
Took me ages to find someone who just EXPLAINED WHAT THE OUTPUT MEANT. Thanks.
Mohanraj     (28 Apr 2010, 22:24)
Really a very good explaination. Thanks!
Pankaj     (19 Apr 2010, 04:35)
nice explanation
Bob Rankin     (13 Mar 2010, 08:39)
The 5c5/7d6, etc. shown above is just a sample of the output from the diff command.
Test     (12 Mar 2010, 04:15)
Which flag can help me get rid of 5c5 / 7d6 / 21a22. i using linux
SaefUllah     (22 Feb 2010, 11:08)
Thanks for making things easy for me
Esteban Talavera     (10 Feb 2010, 09:30)
Thanks!

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.
*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 
 


Ask Bob Rankin - Free Tech Support


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