diffCompare files and directories line by line
diff shows exactly which lines differ between two files or two directory trees. Use it to review a config change before applying it, to find drift between servers, to produce patches, and in scripts, where its exit status says same (0), different (1) or trouble (2).
diff [OPTION]... FILE1 FILE2 diff -r [OPTION]... DIR1 DIR2Before deploying a new nginx config you want to see exactly what changed from the current one.
diffGNU diffutils: computes the smallest set of line changes that turns FILE1 into FILE2.
diff reads both files, splits them into lines and finds a longest common subsequence using Eugene Myers' O(ND) algorithm with heuristics for large inputs: the lines both files share in the same order stay, everything else becomes deletions and insertions. Changes are grouped into hunks. The default 'normal' format prints each hunk as a command like 2c2 (change line 2 to line 2), 3a4 (after line 3 add line 4) or 5d4 (delete), with < for lines from the first file and > for the second. -u prints unified hunks with 3 lines of context, which is easier to read and is what patch and code review tools expect. Options like -i, -w, -b and --strip-trailing-cr normalise lines before comparing. With -r diff walks two directory trees, pairs files by name, reports 'Only in' for unmatched ones and diffs the rest; binary files are only reported as different. Exit status: 0 identical, 1 different, 2 error.
-u / -U NUnified format with 3 (or N) context lines-y -W 120Side by side, 120 columns wide (| changed, < > only on one side)-rCompare directories recursively-qOnly report whether files differ-sAlso report when files are identical-iIgnore case-w / -bIgnore all whitespace / changes in the amount of whitespace--strip-trailing-crIgnore Windows \r line endings-NWith -r, treat missing files as empty (to create full patches)--color=autoRed deletions, green additions-x PATTERNWith -r, exclude files matching the patternif diff a b; then echo different; fiExit status 0 means identical, so the branch runs when the files are the same. And 2 (error) is also 'false'.
diff -q a b >/dev/null; case $? in 0) same;; 1) different;; *) error;; esacReading diff's < and > as 'older' and 'newer' regardless of argument order< is always FILE1 and > FILE2, whatever their dates.
Always pass old first, new second: diff old newUsing diff on two large binary files to find what changeddiff only says 'Binary files differ'.
cmp -l for differing bytes, sha256sum to compare, or diff <(xxd a) <(xxd b)diff -w on YAML or Python filesIgnoring whitespace hides indentation changes, which change the structure or logic.
Plain diff -u, or a structure-aware diff (yq, dyff) for YAMLTry these on a Linux machine of your own. Each task has a hint and a solution.
Copy /etc/app/app.conf to ~/app.conf.new, change workers to 8 in the copy, then show the change in unified format.
Check whether ~/logs/app.log and /var/log/app/app.log are identical and print the exit status.
Compare the ~/logs and /var/log/app directories and list only which files differ or exist on one side.
Compare the server lists in ~/data/servers.txt and a sorted copy, side by side.
diffCompare files and directories line by line
diff shows exactly which lines differ between two files or two directory trees. Use it to review a config change before applying it, to find drift between servers, to produce patches, and in scripts, where its exit status says same (0), different (1) or trouble (2).
diff [OPTION]... FILE1 FILE2 diff -r [OPTION]... DIR1 DIR2Before deploying a new nginx config you want to see exactly what changed from the current one.
diffGNU diffutils: computes the smallest set of line changes that turns FILE1 into FILE2.
diff reads both files, splits them into lines and finds a longest common subsequence using Eugene Myers' O(ND) algorithm with heuristics for large inputs: the lines both files share in the same order stay, everything else becomes deletions and insertions. Changes are grouped into hunks. The default 'normal' format prints each hunk as a command like 2c2 (change line 2 to line 2), 3a4 (after line 3 add line 4) or 5d4 (delete), with < for lines from the first file and > for the second. -u prints unified hunks with 3 lines of context, which is easier to read and is what patch and code review tools expect. Options like -i, -w, -b and --strip-trailing-cr normalise lines before comparing. With -r diff walks two directory trees, pairs files by name, reports 'Only in' for unmatched ones and diffs the rest; binary files are only reported as different. Exit status: 0 identical, 1 different, 2 error.
-u / -U NUnified format with 3 (or N) context lines-y -W 120Side by side, 120 columns wide (| changed, < > only on one side)-rCompare directories recursively-qOnly report whether files differ-sAlso report when files are identical-iIgnore case-w / -bIgnore all whitespace / changes in the amount of whitespace--strip-trailing-crIgnore Windows \r line endings-NWith -r, treat missing files as empty (to create full patches)--color=autoRed deletions, green additions-x PATTERNWith -r, exclude files matching the patternif diff a b; then echo different; fiExit status 0 means identical, so the branch runs when the files are the same. And 2 (error) is also 'false'.
diff -q a b >/dev/null; case $? in 0) same;; 1) different;; *) error;; esacReading diff's < and > as 'older' and 'newer' regardless of argument order< is always FILE1 and > FILE2, whatever their dates.
Always pass old first, new second: diff old newUsing diff on two large binary files to find what changeddiff only says 'Binary files differ'.
cmp -l for differing bytes, sha256sum to compare, or diff <(xxd a) <(xxd b)diff -w on YAML or Python filesIgnoring whitespace hides indentation changes, which change the structure or logic.
Plain diff -u, or a structure-aware diff (yq, dyff) for YAMLTry these on a Linux machine of your own. Each task has a hint and a solution.
Copy /etc/app/app.conf to ~/app.conf.new, change workers to 8 in the copy, then show the change in unified format.
Check whether ~/logs/app.log and /var/log/app/app.log are identical and print the exit status.
Compare the ~/logs and /var/log/app directories and list only which files differ or exist on one side.
Compare the server lists in ~/data/servers.txt and a sorted copy, side by side.