diff
what changed between two filesdiff reports the differences between two files. If they are identical it prints nothing; otherwise it shows which lines to remove and which to add to turn the first into the second.
Lines starting - are only in the old file; lines starting + are only in the new.
At the terminal
Read the session first, then the notes: each numbered line in the output is answered by the note with the same number.
What the numbered lines are telling you
--- is the old file and +++ the new, each with its modification time.
The hunk header: seven lines starting at line 12 in each file.
The line with - was removed and the line with + was added. Lines starting with a space are unchanged context.
No detail — useful in a script, or when comparing many files.
Switches
A switch changes how the command behaves. These are the ones used day to day; man diff lists the rest. Single-letter switches can usually be joined, so -l -a may be written -la.
-u- Unified format, with lines of context. The readable one.
-r- Recursive: compare directories and everything in them.
-q- Brief: report only whether the files differ.
-y- Side by side, in two columns.
-i- Ignore differences in case.
-w- Ignore all differences in spacing.
Variations worth knowing
The same tool, pointed at a narrower question. These four cover most of what it is reached for day to day.
diff -r dir1 dir2Compares two directory trees, file by file.
diff -y a.txt b.txtShows the two files side by side.
diff <(ssh host1 cat /etc/hosts) <(ssh host2 cat /etc/hosts)Compares a file on two machines.
diff -u old new > change.patchSaves the differences as a patch file.
In cybersecurity
Knowing exactly what changed is the core of both review and investigation.
After editing a configuration file, diff it against the backup. An unexpected changed line is caught before the service is restarted.
Comparing a file with a known-good copy from a backup or the package shows precisely what an intruder altered.
Save the list of users, listening ports or cron jobs each day and diff today's against yesterday's: additions stand out.