The diff command is a very important tool on Linux for comparing the contents of files, especially between two different versions of a file to find changes. Diff prints each line change on the command line. The latest version of diff also supports binaries. The output of the diff program is called a patch, because Linux also has a patch program that updates the contents of the a.c file to b.c based on the output of the diff program. Diff is an integral part of version control tools like SVN, CVS, Git, etc.

The command format

Diff [parameter][file 1 or directory 1][file 2 or directory 2]

Command function

The diff command compares the contents of a single file or directory. If you specify a file to compare, this is only valid if the input is a text file. Compare and contrast text files in a line-by-line manner. If you specify a directory to compare, the diff command compares text files with the same name in both directories. Lists different binaries, common subdirectories, and files that only appear in one directory.

The command parameter

  • -< line count > specifies how many lines of text to display. This parameter must be used in conjunction with the -c or -u arguments.
  • -a or –text diff only compares text files line by line by default.
  • -b or –ignore-space-change does not check for differences in space characters.
  • -b or –ignore-blank lines do not check for blank lines.
  • -C displays all the text and marks the differences.
  • -c or –context is the same as executing the “-c-” directive.
  • -D or –minimal uses a different algorithm and is compared in smaller units.
  • -d or ifdef The output format of this parameter is available for preprocessor macros.
  • -e or — Ed The output format of this parameter can be used in Ed’s script file.
  • -for -forward-ed outputs a script file formatted like Ed, but in the same order as the original.
  • -h or –speed-large-files can speed things up when comparing large files.
  • -l or — ignore-match-lines If two files differ on lines that both contain characters or strings specified in the option, the difference between the two files will not be shown.
  • -i or –ignore-case does not check for case differences.
  • -l or –paginate sends the results to the PR program for paging.
  • -n or — RCS displays the comparison results in RCS format.
  • -n or –new-file When comparing directories, if file A appears in Only one directory, the default is: Only in directory: file A If -n is used, diff will compare file A with an empty file.
  • -p Displays the function name where the difference occurs if the file being compared is a C code file.
  • -p or –unidirectional new-file is similar to -n, but is compared to an empty file only if the second directory contains a file that the first directory does not.
  • -Q or — BRIEF only displays differences, not detailed information.
  • -r or –recursive to compare files in subdirectories.
  • -s or — report-ideas-files still displays information if no differences are found.
  • -s or — STARTING -FILE starts the comparison from the specified file when comparing directories.
  • -t or –expand-tabs Expand the TAB character on output.
  • -t or –initial-tab precedes each line with a TAB character for alignment.
  • -u, -u or –unified= shows the differences in file contents in a consolidated manner.
  • -v or –version displays version information.
  • -w or –ignore-all-space ignores all space characters.
  • -w or –width Specifies the column width when using the -y parameter.
  • -x or –exclude from comparing the file or directory specified in the option.
  • -x or –exclude-from You can save the file or directory type as a text file and then specify the text file in =.
  • -y or –side-by-side displays file similarities and differences in a side-by-side manner.
  • –help shows help.
  • –left-column When the -y parameter is used, if a line is the same in two files, the line is displayed only in the left column.
  • — suppressed common-lines displays only the differences when using the -y parameter.

diffthenormalThe display format has three prompts:

  • a – add
  • c – change
  • d – delete

Compare two files

> cat-n 1.txt 12 123 3 Rumenz > cat-n 2.txt 12 456 3 123 45 > diff 1.txt 2.txt 1A2 > 456 3c4,5 < Rumenz -- > > entry
  • 1a2said1.txtAdd after line 1 of the456and2.txtLine 2 of the file is the same.
  • 3 c4, 5Said it would1.txtSo line 3 of the2.txtThe two files are equal after lines 4 and 5 of the

Side by side format output

> the diff 1. TXT 2. TXT - y - 50 W station in the introduction Introduction to station >, 456, 123, 123 rumenz | > get started
  • |Indicates that the contents of the two files are different
  • <Indicates that the following file has 1 line less content than the previous file
  • >Indicates that the following file has one more line than the previous file

Context output format

> diff 1.txt 2.txt-c *** 1.txt 2021-02-28 22:19:02.872483347 +0800 -- -- 2.txt 2021-02-28 22:19:29.834776319 +0800 *************** *** 1,3 new entry station 123! Rumenz -- 1,5 ---- entry station + 456 123! ! An introduction to
  • +The latter of the compared files is one more line than the previous
  • -The latter of the compared files is one line less than the previous
  • !The files compared have different lines

Uniform format output

> diff 1.txt 2.txt-u -- -- 1.txt 2021-02-28 22:19:02.872483347 +0800 +++ 2.txt 2021-02-28 22:19:29.834776319 +0800 @@ -1,3 +1,5 @@ Starter Station +456 123 - Rumenz + + Starter

The first part

  • — 1.txt 2021-02-28 22:19:02.872483347 +0800
  • +++ 2.txt 2021-02-28 22:19:29.834776319 +0800
  • “– “represents the document before the change, and “+++” represents the document after the change.

The second part

The changing position starts and ends with two @’s

  • @ @ – 1, 3 + 1, 5 @ @

In front of
- 1, 3It is divided into three parts: the minus sign represents the first file (that is, 1.txt), “1” represents the first line, and “3” represents three lines in a row. Taken together, this means that the following are three lines in a row from line 1 of the first file. Similarly, “+1,5” means that after the change, it will be 5 lines in a row starting from line 1 in the second file.

Compare two files differently and produce patches

> diff-run 1.txt 2.txt > cat patch.log -- -- 1.txt 2021-02-28 22:19:02.872483347 +0800 +++ 2.txt 2021-02-28 22:19:29.834776319 +0800 @@-1,3 +1,5 @@Starter +456 123-Rumenz + + Starter

Patch to make both files the same

Log patching file 1. TXT > cat 1. TXT cat 2. TXT cat 2

Compare catalog differences

> tree SRC/SRC / ├─ sigar ─ sigar ─ sigar ─ sigar ─ sigar ─ sigar ─ sigar ─ sigar 0 files bb0 tree DST/DST/sigma ─ test1 ├─ test2 sigma ─ one sigma ─ test3 directories, 0 files > diff -r src/ dst/ Only in src/test1: one Only in dst/test2: one

The original link: https://rumenz.com/rumenbiji/… WeChat official account: entry station