Q1In diff's unified-format output, which symbol starts a removed line (one that was in the old file)?
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.
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.
| Symbol | Meaning |
|---|---|
--- old.txt | Name of the first (old) file compared |
+++ new.txt | Name of the second (new) file compared |
@@ -N,M +N,M @@ | Position info for where the change happened |
- line | A removed line (was in the old file) |
+ line | An added line (is in the new file) |
line (leading space) | An unchanged line shown together as context |
@@ 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
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
diff base.txt more.txt shows +three (added), while the reversed diff more.txt base.txt shows -three (removed).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.
Knowledge Check
Answer each question one by one.
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?