Comparing files and patches
Quick answer
diff describes how text files differ; cmp compares bytes. A patch is a description of changes that a patch tool can apply. Work on a copy, inspect the change and verify the resulting file.
Prepare the practice environment
Use the chapter fixture described in lab-fixtures/search-transform/README.md. Run setup as a normal user in a disposable Linux environment, enter the printed directory and check pwd. Every example assumes a fresh fixture. Commands omit the terminal prompt; outputs below are not commands.
How it works
GNU diff -u produces unified differences, with --- and +++ headers, @@ ranges, context lines and lines prefixed by - (old) or + (new). diff returns 0 for equal inputs, 1 for differences, 2 for trouble. cmp -s uses 0 for identical, 1 for different and 2 for trouble. The fixture contains a small, reviewed patch with labels old.txt and new.txt. Giving patch an explicit target and --dry-run checks whether it can apply; it does not prove the patch is trustworthy. Apply only to the fixture copy. --batch and --forward avoid prompts and reversed application in this GNU patch example.
Comparison is a question, not a repair: exit status 1 can be the expected result when two files differ. A dry run of a patch checks whether it appears applicable to the selected copy, but does not prove that the result has the intended meaning. Compare the changed copy with the trusted expected file and keep the original available for recovery.
Try the examples
diff -u --label old.txt --label new.txt old.txt new.txtprintf 'status=%s\n' "$?"The limit changed, and status 1 reports a difference rather than a tool error.
Expected stdout:
--- old.txt+++ new.txt@@ -1,2 +1,2 @@ mode=demo-limit=2+limit=5status=1Quiet comparison prints no diff; use its status.
Expected stdout:
status=1cat change.patchcp old.txt work.txtpatch --dry-run --batch --forward work.txt < change.patch &&patch --batch --forward work.txt < change.patch &&cmp -s work.txt new.txt && printf '%s\n' verifiedRead the patch before running the remaining lines. Dry run, apply and byte comparison must all succeed. The original is unchanged.
Expected stdout:
--- old.txt+++ new.txt@@ -1,2 +1,2 @@ mode=demo-limit=2+limit=5checking file work.txtpatching file work.txtverifiedCommon mistakes
Never apply an unknown patch to a real project or system files. A clean dry run only checks applicability. Review contents, target and backup first.
Practice
Copy new.txt to same.txt and use cmp -s to verify equality. Then compare old.txt and new.txt and explain the changed line.
Related references
Study: diff, cmp, patch. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Comparing files and patches — Knowledge Check
Apply the lesson concepts to the controlled text fixture.
What does diff status 1 mean?
What does a + line in a unified diff represent?
What does patch --dry-run establish?
Which tool compares bytes quietly?