diff: compare files line by line
diff [OPTION]... FILE1 FILE2
Options you will use
-u, -U N, --unified[=N]- Unified format: --- / +++ headers, @@ -start,count +start,count @@ hunks, N lines of context (default 3). What code review, git and patch use.
-c, -C N, --context[=N]- Context format: *** / --- headers, ! for a changed line, - removed, + added.
-y, --side-by-side- Two columns: | changed, < only on the left, > only on the right.
-W N, --width=N- With -y: at most N columns in all (default 130).
--suppress-common-lines- With -y: do not print the lines that are the same.
-q, --brief- Only say whether the files differ (exit status 1), no details.
-s, --report-identical-files- Say so when two files are the same.
-r, --recursive- Compare two directories, and their subdirectories, file by file. "Only in DIR: NAME" = a file one side does not have.
-N, --new-file- A file missing on one side counts as empty: its whole content shows as added or removed.
-i, --ignore-case- Upper and lower case count as the same.
-w, --ignore-all-space- Ignore all white space when comparing lines.
-b, --ignore-space-change- Ignore changes in the amount of white space (and trailing white space).
-B, --ignore-blank-lines- Ignore changes that only add or remove empty lines.
-a, --text- Compare as text even if a file looks binary (has NUL bytes).
-x PAT, --exclude=PAT- With -r: skip files whose name matches PAT.
--label LABEL- Use LABEL instead of the file name and time in the header (give it twice for both files).
--color[=WHEN]- Colour the output: never (default), always, auto (= only on a terminal; --color alone means auto).
Examples
$ diff -u app.conf.bak app.confwhat changed since the backup, in the review format
$ diff -r overlays/dev overlays/prodevery difference between two directories
$ diff <(sort a.txt) <(sort b.txt)compare two outputs without temp files
$ diff -u old.conf new.conf > fix.diffsave a patch for patch -p0 < fix.diff
Gotchas
- Exit status: 0 the same, 1 different, 2 trouble (a missing file, a bad option, or binary files that differ without -q). So in a script
if ! diff -q a b >/dev/null; thenmeans "if they differ". - Normal format: 3c3 = line 3 of the first file changed into line 3 of the second (a = added, d = deleted); < lines come from the first file, > lines from the second.
- "\ No newline at end of file" marks a last line without its newline: such a file differs from the same text with one.
Taught in
Try diff in a real terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.