diff -u before.txt after.txt prints the differences between two files as a unified diff, the format git diff and patch use: removed lines start with -, added lines with +, and three unchanged lines of context surround each change.
diff -u before.txt after.txt
--- before.txt 2026-10-15 09:00:00.000000000 +0000
+++ after.txt 2026-10-15 09:30:00.000000000 +0000
@@ -1,5 +1,6 @@
name: api
-image: api:1.4
+image: api:1.5
replicas: 2
port: 8080
-log: info
+log: debug
+timeout: 30
That’s GNU diffutils 3.10 on Linux. The headers carry each file’s modification time; macOS’s diff prints it as 2026-10-15 09:00:00. The tool below shows the same hunk under Unified diff, headed before and after.
Reading the hunk header
@@ -1,5 +1,6 @@ means: this hunk covers 5 lines of the old file starting at line 1, and 6 lines of the new file starting at line 1. The old side is every line marked with a space or - (3 + 2), the new side every line marked with a space or + (3 + 3). A changed line is a - and a +, so the tool counts 3 lines added and 2 removed.
A count of 1 is left out, and a count of 0 numbers the side from the line before. diff -U0 drops the context and shows it:
@@ -2 +2 @@
-image: api:1.4
+image: api:1.5
@@ -5 +5,2 @@
-log: info
+log: debug
+timeout: 30
Adding one line to an empty file gives @@ -0,0 +1 @@. With the default 3 lines of context, changes 6 or fewer unchanged lines apart share a hunk, in GNU diff, macOS and the tool alike.
Color
diff --color -u before.txt after.txt colors removed lines red and added lines green when the output is a terminal. Through a pager, force it: diff --color=always -u before.txt after.txt | less -R. GNU diff added --color in 3.4; macOS’s diff has it too, and leaves the headers uncolored.
Whitespace: -b and -w
before after -b -w tool
replicas: 2 replicas: 2 same same same
port: 8080 port: 8080␣␣␣ same same same
a␍ (CRLF) a (LF) same same same
␣␣log: info log: info differ same same
name: api name:api differ same differ
In the table, ␣ is a space and ␍ a carriage return. -b ignores changes in the amount of white space, and trailing white space, a CRLF’s carriage return included. -w ignores all of it. GNU and macOS agree on every row. The tool’s Ignore whitespace is -b that also ignores indentation. For line endings alone, GNU has --strip-trailing-cr, and -B ignores added or removed blank lines.
Exit status
diff exits 0 when the files match, 1 when they differ, and more than 1 on trouble, as POSIX specifies: GNU and macOS exit 2 for a missing file. In a script, diff -q a b >/dev/null tests without printing the changes; -q alone prints Files before.txt and after.txt differ.