CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / File Viewing & Editing / diff

diff

Compare files and directories line by line

Intermediateconfigdriftpatchesscripting

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).

Syntaxdiff [OPTION]... FILE1 FILE2 diff -r [OPTION]... DIR1 DIR2

Watch it run

Before deploying a new nginx config you want to see exactly what changed from the current one.

devops@devops-lab: ~/deploy — zsh — 96×28
➜ ~/deploy
Step 1 of 3
diff old/nginx.conf new/nginx.conf

Normal format: each hunk starts with a command. 2c2 means line 2 changed; 11c11,12 means old line 11 became new lines 11-12. < is the old file, > the new.

Anatomy of a command

$
commanddiff

GNU diffutils: computes the smallest set of line changes that turns FILE1 into FILE2.

How it works

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.

Options you'll actually use

-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 pattern

Common mistakes

if diff a b; then echo different; fi

Exit 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;; esac
Reading 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 new
Using diff on two large binary files to find what changed

diff 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 files

Ignoring whitespace hides indentation changes, which change the structure or logic.

Plain diff -u, or a structure-aware diff (yq, dyff) for YAML

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Copy /etc/app/app.conf to ~/app.conf.new, change workers to 8 in the copy, then show the change in unified format.

  2. 2

    Check whether ~/logs/app.log and /var/log/app/app.log are identical and print the exit status.

  3. 3

    Compare the ~/logs and /var/log/app directories and list only which files differ or exist on one side.

  4. 4

    Compare the server lists in ~/data/servers.txt and a sorted copy, side by side.

Related commands

catPrint and concatenate fileswcCount lines, words and bytessedStream editor: find and replace, delete and print linesrsyncSync files and directories efficiently
PreviouswcNextps
Linux / File Viewing & Editing / diff

diff

Compare files and directories line by line

Intermediateconfigdriftpatchesscripting

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).

Syntaxdiff [OPTION]... FILE1 FILE2 diff -r [OPTION]... DIR1 DIR2

Watch it run

Before deploying a new nginx config you want to see exactly what changed from the current one.

devops@devops-lab: ~/deploy — zsh — 96×28
➜ ~/deploy
Step 1 of 3
diff old/nginx.conf new/nginx.conf

Normal format: each hunk starts with a command. 2c2 means line 2 changed; 11c11,12 means old line 11 became new lines 11-12. < is the old file, > the new.

Anatomy of a command

$
commanddiff

GNU diffutils: computes the smallest set of line changes that turns FILE1 into FILE2.

How it works

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.

Options you'll actually use

-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 pattern

Common mistakes

if diff a b; then echo different; fi

Exit 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;; esac
Reading 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 new
Using diff on two large binary files to find what changed

diff 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 files

Ignoring whitespace hides indentation changes, which change the structure or logic.

Plain diff -u, or a structure-aware diff (yq, dyff) for YAML

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Copy /etc/app/app.conf to ~/app.conf.new, change workers to 8 in the copy, then show the change in unified format.

  2. 2

    Check whether ~/logs/app.log and /var/log/app/app.log are identical and print the exit status.

  3. 3

    Compare the ~/logs and /var/log/app directories and list only which files differ or exist on one side.

  4. 4

    Compare the server lists in ~/data/servers.txt and a sorted copy, side by side.

Related commands

catPrint and concatenate fileswcCount lines, words and bytessedStream editor: find and replace, delete and print linesrsyncSync files and directories efficiently
PreviouswcNextps