The diff
command is used to compare files line by line. It outputs the differences between two files, making it easy to identify changes. This command is often used for version control, code review, and file comparison tasks.
Initial Example
Using diff
to compare two files, file1.txt
and file2.txt
:
diff file1.txt file2.txt
DIFF Parameters
Parameter | Description |
---|---|
--normal | Display the output in the default format. |
-q, --brief | Report only if the files differ, without detailed information. |
-s, --report-identical-files | Indicate when two files are identical. |
-c, -C NUM, --context[=NUM] | Show NUM lines of context around each change (default is 3). |
-u, -U NUM, --unified[=NUM] | Display NUM lines of unified context (default is 3). |
-e, --ed | Produce an ed script to recreate changes. |
-n, --rcs | Output the differences in RCS format. |
-y, --side-by-side | Display differences in a two-column format. |
-W, --width=NUM | Set the maximum output width to NUM columns (default is 130). |
--left-column | Show only the left column for lines common to both files. |
--suppress-common-lines | Do not show lines that are common between the files. |
-p, --show-c-function | Display which C function each change occurs in. |
-F, --show-function-line=RE | Display the most recent line matching the regular expression RE. |
--label LABEL | Use LABEL instead of the file name and timestamp (can be repeated). |
-t, --expand-tabs | Expand tabs to spaces in the output. |
-T, --initial-tab | Align output using a tab before the text. |
--tabsize=NUM | Set tab stops every NUM columns (default is 8). |
--suppress-blank-empty | Omit spaces or tabs before empty output lines. |
-l, --paginate | Paginate output through the pr command. |
-r, --recursive | Recursively compare subdirectories found in the comparison. |
--no-dereference | Do not follow symbolic links. |
-N, --new-file | Treat missing files as empty files. |
--unidirectional-new-file | Treat missing files in the first set as empty. |
--ignore-file-name-case | Ignore case differences when comparing file names. |
--no-ignore-file-name-case | Consider case differences when comparing file names. |
-x, --exclude=PAT | Exclude files that match the pattern PAT. |
-X, --exclude-from=FILE | Exclude files that match any pattern in FILE. |
-S, --starting-file=FILE | Start the directory comparison with the file FILE. |
--from-file=FILE1 | Compare FILE1 to all operands; FILE1 can be a directory. |
--to-file=FILE2 | Compare all operands to FILE2; FILE2 can be a directory. |
-i, --ignore-case | Ignore case differences in file contents. |
-E, --ignore-tab-expansion | Ignore changes due to tab expansion. |
-Z, --ignore-trailing-space | Ignore white space at the end of lines. |
-b, --ignore-space-change | Ignore changes in the amount of white space. |
-w, --ignore-all-space | Ignore all white space differences. |
-B, --ignore-blank-lines | Ignore changes where lines are all blank. |
-I, --ignore-matching-lines=RE | Ignore changes where all lines match the regular expression RE. |
-a, --text | Treat all files as text. |
--strip-trailing-cr | Remove trailing carriage return characters from input. |
-D, --ifdef=NAME | Output merged file with #ifdef NAME diff markers. |
--GTYPE-group-format=GFMT | Format GTYPE input groups with the format string GFMT. |
--line-format=LFMT | Format all input lines with the format string LFMT. |
--LTYPE-line-format=LFMT | Format LTYPE input lines with the format string LFMT. |
-d, --minimal | Try to find the smallest set of changes. |
--horizon-lines=NUM | Keep NUM lines of the common prefix and suffix. |
--speed-large-files | Optimize for handling large files with many scattered changes. |
--color[=WHEN] | Use colored output; WHEN can be never , always , or auto . |
--palette=PALETTE | Define colors for use when --color is active; PALETTE is a colon-separated list of terminfo capabilities. |
--help | Display help information and exit. |
-v, --version | Show version information and exit. |
Examples
1. Basic File Comparison
To compare the contents of two files:
diff file1.txt file2.txt
2. Side-by-Side Comparison
To display the differences side by side, use the -y
option:
diff -y file1.txt file2.txt
3. Ignore Case Differences
To ignore case differences in the comparison, use the -i
option:
diff -i file1.txt file2.txt
4. Ignore White Space Changes
To ignore changes in white space, use the -w
option:
diff -w file1.txt file2.txt
5. Compare Directories
To compare the contents of two directories:
diff -r dir1 dir2
6. Unified Format
To display differences in a unified format, which is often used in patch files, use the -u
option:
diff -u file1.txt file2.txt
7. Context Format
To display differences in a context format, use the -c
option:
diff -c file1.txt file2.txt