What does an exclamation mark mean in diff output

diff()

I have an assignment for school. One part of it is to check a file for changes and write these changes to a log file. So far I've found the diff command which could be helpful in my opinion. Let's say I have two files with content like this:

file1

orange
apple

file2

orange
apple
strawberry

If I use diff -c file1 file2 in this case, the output of the command is

*** file1   2016-11-24 08:31:19.424712242 +0100
--- file2   2016-11-24 08:25:24.604681751 +0100
***************
*** 1,2 ****
--- 1,3 ----
  orange
  apple
+ strawberry

which I think says that line with '+' sign needs to be added to file1 for them to be the same(?).

Now let's say I change file1 to this:

orange
apple
peach

The output of diff -c file1 file2 is:

*** file1   2016-11-24 08:34:50.647128312 +0100
--- file2   2016-11-24 08:25:24.604681751 +0100
***************
*** 1,3 ****
  orange
  apple
! peach
--- 1,3 ----
  orange
  apple
! strawberry

And here I'm lost, because I don't understand what these exclamation marks mean. Suddenly, the diff command seems not so helpful. I've tried looking at the man page of diff command, but can't find anything (maybe I just don't see it).

Best Answer

  • diff -u

may be what you need for your assignment.

To take your example and using diff -u:

michael@x071:[/home/michael]diff -u file?
--- file1       2016-11-24 07:48:41 +0000
+++ file2       2016-11-24 07:48:57 +0000
@@ -1,3 +1,3 @@
 orange
 apple
-peach
+strawberry

A word of advice - RTM - or - Read The Manual. There are often other options. FYI: the historic options of diff (and diff3 when comparing three files) were to assist with creating "program inout" that would change file1 into file2 (or file2 back into file1). This has been the base of all "version control" software.

The diff options I remember from long ago:

  • -e : Produces output in a form suitable for use with the ed editor to convert File1 to File2.
  • -f : Produces output in a form not suitable for use with the ed editor, showing the modifications necessary to convert File1 to File2 in the reverse order of that produced under the -e flag.
  • -n : Produces output similar to that of the -e flag, but in the opposite order and with a count of changed lines on each insert or delete command. This is the form used by the revision control system (RCS).

The last option I will highlight is a "new" one - relatively speaking. (also several years old but was often not in POSIX implementations). Rather than creating output suitable for 'ed' of 'RCS', this is suitable for patch:

  • -u : Produces a diff command comparison with three lines of unified context. The output is similar to that of the -c flag, except that the context lines are not repeated; instead, the context, deleted, and added lines are shown together, interleaved.

IMHO: the key value of diff -c is as an improvement over the command cmp - when you want to know more than ONLY if two files differ, or not. I had never paid attention (maybe it is a "new" option as well) - but shall think about it when my question is a recursive search for files that differ between two directory trees.

Related Question