Linux – diff Command

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.



Tutorials dojo strip

Initial Example

Using diff to compare two files, file1.txt and file2.txt:

diff file1.txt file2.txt




DIFF Parameters

ParameterDescription
--normalDisplay the output in the default format.
-q, --briefReport only if the files differ, without detailed information.
-s, --report-identical-filesIndicate 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, --edProduce an ed script to recreate changes.
-n, --rcsOutput the differences in RCS format.
-y, --side-by-sideDisplay differences in a two-column format.
-W, --width=NUMSet the maximum output width to NUM columns (default is 130).
--left-columnShow only the left column for lines common to both files.
--suppress-common-linesDo not show lines that are common between the files.
-p, --show-c-functionDisplay which C function each change occurs in.
-F, --show-function-line=REDisplay the most recent line matching the regular expression RE.
--label LABELUse LABEL instead of the file name and timestamp (can be repeated).
-t, --expand-tabsExpand tabs to spaces in the output.
-T, --initial-tabAlign output using a tab before the text.
--tabsize=NUMSet tab stops every NUM columns (default is 8).
--suppress-blank-emptyOmit spaces or tabs before empty output lines.
-l, --paginatePaginate output through the pr command.
-r, --recursiveRecursively compare subdirectories found in the comparison.
--no-dereferenceDo not follow symbolic links.
-N, --new-fileTreat missing files as empty files.
--unidirectional-new-fileTreat missing files in the first set as empty.
--ignore-file-name-caseIgnore case differences when comparing file names.
--no-ignore-file-name-caseConsider case differences when comparing file names.
-x, --exclude=PATExclude files that match the pattern PAT.
-X, --exclude-from=FILEExclude files that match any pattern in FILE.
-S, --starting-file=FILEStart the directory comparison with the file FILE.
--from-file=FILE1Compare FILE1 to all operands; FILE1 can be a directory.
--to-file=FILE2Compare all operands to FILE2; FILE2 can be a directory.
-i, --ignore-caseIgnore case differences in file contents.
-E, --ignore-tab-expansionIgnore changes due to tab expansion.
-Z, --ignore-trailing-spaceIgnore white space at the end of lines.
-b, --ignore-space-changeIgnore changes in the amount of white space.
-w, --ignore-all-spaceIgnore all white space differences.
-B, --ignore-blank-linesIgnore changes where lines are all blank.
-I, --ignore-matching-lines=REIgnore changes where all lines match the regular expression RE.
-a, --textTreat all files as text.
--strip-trailing-crRemove trailing carriage return characters from input.
-D, --ifdef=NAMEOutput merged file with #ifdef NAME diff markers.
--GTYPE-group-format=GFMTFormat GTYPE input groups with the format string GFMT.
--line-format=LFMTFormat all input lines with the format string LFMT.
--LTYPE-line-format=LFMTFormat LTYPE input lines with the format string LFMT.
-d, --minimalTry to find the smallest set of changes.
--horizon-lines=NUMKeep NUM lines of the common prefix and suffix.
--speed-large-filesOptimize for handling large files with many scattered changes.
--color[=WHEN]Use colored output; WHEN can be never, always, or auto.
--palette=PALETTEDefine colors for use when --color is active; PALETTE is a colon-separated list of terminfo capabilities.
--helpDisplay help information and exit.
-v, --versionShow 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




Linux Playground

Scroll to Top