How to use the diff command in Linux
You can use the diff command to show differences between two files, or each corresponding file in two directories. diff outputs differences between files line by line in any of several formats, selectable by command line options. This set of differences is often called a “diff” or “patch”. For files that are identical, diff normally produces no output; for binary (non-text) files, diff normally reports only that they are different.
1,2c1 < abcd < dxxxf --- > hihiih #diff -v diff (GNU diffutils) 3.3
To view differences in context mode, use the -c option.
*** fileone 2021-07-04 17:49:47.618438022 +0800 --- filetwo 2021-07-04 17:49:31.594436803 +0800 *************** *** 1,2 **** ! abcd ! dxxxf --- 1 ---- ! hihiih
To view differences in unified mode, use the -u option.
--- fileone 2021-07-04 17:49:47.618438022 +0800 +++ filetwo 2021-07-04 17:49:31.594436803 +0800 @@ -1,2 +1 @@ -abcd -dxxxf +hihiih
The syntax for the diff command is as follows:
Usage:
diff [OPTION]... FILES
OPTIONS:
--normaloutput a normal diff (the default)-q, --briefreport only when files differ-s, --report-identical-filesreport when two files are the same-c, -C NUM, --context[=NUM]output NUM (default 3) lines of copied context-u, -U NUM, --unified[=NUM]output NUM (default 3) lines of unified context-e, --edoutput an ed script-n, --rcsoutput an RCS format diff-y, --side-by-sideoutput in two columns-W, --width=NUMoutput at most NUM (default 130) print columns--left-columnoutput only the left column of common lines--suppress-common-linesdo not output common lines-p, --show-c-functionshow which C function each change is in-F, --show-function-line=REshow the most recent line matching RE--label LABELuse LABEL instead of file name (can be repeated)-t, --expand-tabsexpand tabs to spaces in output-T, --initial-tabmake tabs line up by prepending a tab--tabsize=NUMtab stops every NUM (default 8) print columns--suppress-blank-emptysuppress space or tab before empty output lines-l, --paginatepass output through 'pr' to paginate it-r, --recursiverecursively compare any subdirectories found--no-dereferencedon't follow symbolic links-N, --new-filetreat absent files as empty--unidirectional-new-filetreat absent first files as empty--ignore-file-name-caseignore case when comparing file names--no-ignore-file-name-caseconsider case when comparing file names-x, --exclude=PATexclude files that match PAT-X, --exclude-from=FILEexclude files that match any pattern in FILE-S, --starting-file=FILEstart with FILE when comparing directories--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 line end-b, --ignore-space-changeignore changes in the amount of white space-w, --ignore-all-spaceignore all white space-B, --ignore-blank-linesignore changes where lines are all blank-I, --ignore-matching-lines=REignore changes where all lines match RE-a, --texttreat all files as text--strip-trailing-crstrip trailing carriage return on input-D, --ifdef=NAMEoutput merged file with '#ifdef NAME' diffs--GTYPE-group-format=GFMTformat GTYPE input groups with GFMT--line-format=LFMTformat all input lines with LFMT--LTYPE-line-format=LFMTformat LTYPE input lines with LFMT-d, --minimaltry hard to find a smaller set of changes--horizon-lines=NUMkeep NUM lines of the common prefix and suffix--speed-large-filesassume large files and many scattered small changes--helpdisplay this help and exit-v, --versionoutput version information and exit
FILES are 'FILE1 FILE2' or 'DIR1 DIR2' or 'DIR FILE...' or 'FILE... DIR'. If --from-file or --to-file is given, there are no restrictions on FILE(s). If a FILE is '-', read standard input. Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.