Learn by reading through in order

File Differences — diff

See how to check the differences between two files with diff. You'll read which lines were added, removed, or changed, using diagrams and a hands-on terminal right in your browser.

When to use diff

Use it when you want to check what you changed after editing a file, or whether two files have the same contents. Give it two files and it shows only the lines that differ.

Comparing Two Files — diff

diff compares two files and shows only the parts that differ. You use it as diff old.txt new.txt, treating the first file as the old (before) version and the second as the new (after) version. Identical lines aren't printed — only the differences are reported.

Comparing two files with diff
old.txtapple / banana / cherrynew.txtapple / grape / cherrydiffOutput-banana+grapeoldnewshows the diffthe two files compared
diff old.txt new.txt lines up the two files and shows only the differing second line as -banana (old) and +grape (new). The identical apple and cherry aren't printed.

In the browser terminal, diff shows differences in unified format. The leading --- and +++ are the names of the two files compared, the @@ line marks where the change happened, and in the body each removed line starts with - and each added line with +. Unchanged lines start with a space and appear alongside as context.

SymbolMeaning
--- old.txtName of the first (old) file compared
+++ new.txtName of the second (new) file compared
@@ -N,M +N,M @@Position info for where the change happened
- lineA removed line (was in the old file)
+ lineAn added line (is in the new file)
line (leading space)An unchanged line shown together as context
Reading the unified format
Removed line(old file)@@ -1,3 +1,3 @@Marks where thechange happened- banana+ grapeAdded line(new file)
In unified format, @@ marks the change position, - a removed line (old), and + an added line (new). Unchanged lines appear alongside with a leading space.
printf 'apple\nbanana\ncherry\n' > old.txt   # create the sample
printf 'apple\ngrape\ncherry\n' > new.txt    # a sample that differs by one line
diff old.txt new.txt
--- old.txt
+++ new.txt
@@ -1,3 +1,3 @@
 apple
-banana
+grape
 cherry

① Run printf 'apple\nbanana\ncherry\n' > old.txt to create the first sample file.

② Run printf 'apple\ngrape\ncherry\n' > new.txt to create the second sample file, which differs only on line 2.

③ Pass the two files to diff in this order and read which lines get the - (removed) and + (added) markers. (Run it correctly and the explanation will appear.)

Linux console
0 / 3 completed
Loading Linux Terminal...

Reading Additions and Deletions

Differences aren't only rewrites. When a line is added only in the right file, you get just a + line; when a line is removed from the left file, you get just a - line. Instead of - and + appearing as a pair like in a rewrite, only one of them showing up is the sign of an addition or deletion. Swap the order you compare in, and the same difference flips between + (added) and - (removed).

printf 'one\ntwo\n' > base.txt        # create the sample
printf 'one\ntwo\nthree\n' > more.txt  # a sample with one line added
diff base.txt more.txt
--- base.txt
+++ more.txt
@@ -1,2 +1,3 @@
 one
 two
+three
Swapping the order flips + and -
diffbase.txt more.txtSwap the order and+ and - flipdiffmore.txt base.txt+ three(added)- three(removed)+ appears- appears
With the same two files, diff base.txt more.txt shows +three (added), while the reversed diff more.txt base.txt shows -three (removed).

① Run printf 'one\ntwo\n' > base.txt to create the first, two-line sample.

② Run printf 'one\ntwo\nthree\n' > more.txt to create the second sample with a third line added.

③ Compare with diff in the order base.txtmore.txt and confirm the added line starts with +.

④ Then reverse the order to more.txtbase.txt and confirm the same difference shows as - (removed), and that swapping the order flips + and -.

Linux console
0 / 4 completed
Loading Linux Terminal...

diff and patch, and how other environments display it

The unified-format diff that diff produces can be applied to another file as-is with the patch command. When a team exchanges fixes, passing this diff (a patch) back and forth is common. This course focuses on reading diffs and doesn't cover applying them here.

In this course's browser terminal, diff outputs unified format directly. With GNU diff on Ubuntu and the like, plain diff shows the traditional format using 2c2 and < / >, and adding diff -u gives the same unified format used in this course.

QUIZ

Knowledge Check

Answer each question one by one.

Q1In diff's unified-format output, which symbol starts a removed line (one that was in the old file)?

Q2What does a line like @@ -1,3 +1,3 @@ show?

Q3When a line is added only in the right file, what does diff's output look like?