diff — File & Output Comparison
Comparison patterns — unified diff, side-by-side, process substitution for comparing command outputs, and preview-before-apply with sed | diff.
Comparison Patterns
Basic file diff
diff file-a.txt file-b.txt
# Lines prefixed with < are from file-a, > from file-b
Unified diff (most readable)
diff -u original.adoc modified.adoc
# - lines removed, + lines added, @@ context markers
# Same format as git diff
Side-by-side comparison
diff -y --width=120 file-a.txt file-b.txt
# | marks changed lines, < and > mark additions/deletions
Process substitution — compare command outputs without temp files
diff <(grep -rlP 'include::partial\$' docs/modules/ROOT/pages/) \
<(grep -rlP 'include::example\$' docs/modules/ROOT/pages/)
# Compare which pages use partials vs examples
# <(...) creates a virtual file from command output
Preview sed changes before applying
sed 's/placeholder/POPULATED/' file.adoc | diff file.adoc -
# - reads from stdin (sed output)
# Shows exactly what sed would change
Compare sorted outputs — before/after validation
diff <(sort before.txt) <(sort after.txt)
# Structural comparison ignoring order
Quiet mode — just check if files differ
diff -q file-a file-b
# Output: "Files file-a and file-b differ" or nothing
# Exit code: 0 = same, 1 = different
if diff -q file-a file-b > /dev/null 2>&1; then
echo "Files are identical"
fi
Recursive directory diff
diff -rq dir-a/ dir-b/
# Lists files that differ, exist only in one dir, etc.
See Also
-
sed — preview changes with
sed … | diff file - -
Bash Pipes — process substitution
<()for virtual files