Learn to create and apply diff patches on the command line to update files reliably. 16.11.2025 | reading time: 2 min Why rewrite a whole file when a small change will do? The `patch` command reads a diff file and updates files automatically, which is perfect for transporting targeted fixes, sharing changes by email or maintaining small source tweaks. A simple scenario Create two quick files with `printf "one\ncommon\n" > original.txt` and `printf "one\nchanged\n" > modified.txt`, then generate a unified diff with `diff -u original.txt modified.txt > change.patch`; apply it with `patch -p0 < change.patch` which typically prints `patching file original.txt` and replaces the differing line so the file now matches the modified version. Flags and common options Strip path components with `-pNUM` (for example `-p1`), test without changing files using `--dry-run`, reverse a patch with `-R`, create backups with `-b` and accept or force hunks with `--force`; unified diffs (`diff -u`) are the de facto standard and `patch` handles them best. When patches fail If hunks do not apply `patch` leaves `.rej` reject files and you can re-run with `--dry-run` to diagnose; inspect the context lines, adjust the source manually, or fall back to `git apply` or manual merging when offsets are too large or files were heavily refactored. Related workflows Use `diff` to create patches, `patch` to apply them, and `git` for project history — for series of changes consider `quilt` or `git format-patch`/`git am` for email workflows; automated CI systems often apply patches in test jobs before committing. Next steps Start practicing by creating, applying and reversing small patches until the options feel natural; then explore integrating patches into version-control and CI pipelines and consider deepening Linux skills with certifications like CompTIA Linux+ or LPIC-1 and intensive exam preparation at bitsandbytes.academy. Join Bits & Bytes Academy First class LINUX exam preparation. utilities version-control scripting