Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Bash Dictionary
  3. diff / patch

diff / patch

diff compares two files or directories and shows the differences between them. patch applies a diff output (a patch file) to a file. Both commands are used for version control and software updates.

Syntax

diff [options] file1 file2
patch [options] [original-file] [patch-file]

Options

Command / OptionDescription
diff file1 file2Shows differences between two files. Lines marked with < are from file1; lines marked with > are from file2.
diff -u file1 file2Shows differences in unified format — the same style used by git diff.
diff -r dir1 dir2Recursively compares two directories.
diff -i file1 file2Ignores case differences when comparing.
diff -b file1 file2Ignores differences in the amount of whitespace.
diff -q file1 file2Reports only whether the files differ, without showing the actual differences.
diff -y file1 file2Shows the comparison in two side-by-side columns.
patch file < patchApplies the differences in a patch file to the specified file.
patch -R file < patchReverses a patch (undoes the applied changes).
patch -p1 < patchStrips the first directory component from file paths in the patch before applying it.

Sample Code

The following examples use these two files.

original.txt
line1
line2
line3
modified.txt
line1
line2 modified
line3
line4 added

Use diff -u to display the differences in unified format (the same style as git diff).

diff -u original.txt modified.txt
--- original.txt	2026-03-05 10:00:00.000000000 +0900
+++ modified.txt	2026-03-05 10:01:00.000000000 +0900
@@ -1,3 +1,4 @@
 line1
-line2
+line2 modified
 line3
+line4 added

Save the differences as a patch file.

diff -u original.txt modified.txt > changes.patch

Apply the patch file to the original.

patch original.txt < changes.patch
patching file original.txt

Use -R to reverse the patch and restore the file to its previous state.

patch -R original.txt < changes.patch
patching file original.txt

Use -q to check only whether files differ — useful for conditionals in scripts.

diff -q original.txt modified.txt
Files original.txt and modified.txt differ

Use -r to recursively compare two directories.

diff -r dir1/ dir2/
diff -r dir1/config.txt dir2/config.txt
1c1
< debug=false
---
> debug=true
Only in dir2/: newfile.txt

Use -y to display the comparison in two side-by-side columns.

diff -y original.txt modified.txt
line1                 line1
line2               | line2 modified
line3                 line3
                    > line4 added

Notes

In unified format ('-u'), lines beginning with - were removed and lines beginning with + were added. This is the same diff format used internally by Git. In modern software development, you rarely call diff and patch directly — Git handles all of that for you. However, being able to read their output is essential for applying patches on a server or understanding the output of code review tools.

When comparing files and copying them, see also cp.

If you find any errors or copyright issues, please .