Detailed explanation of Linux system diff file comparison command

diffcommand is used to compare the differences between two files or directories. It compares the contents of files line by line and displays differences on different lines. diffThe usage and options of the command are as follows :

Basic syntax:

diff [选项] 文件1 文件2

Common options:

  • -cor --context: show context differences (3 lines by default)
  • -uor --unified: show diffs in uniform format
  • -ror --recursive: recursively compare files in directories
  • -qOr --brief: only display whether there is a difference in the file, do not display the specific content of the difference
  • -ior --ignore-case: ignore differences in case
  • -wor --ignore-all-space: ignore all whitespace characters for differences
  • -Bor --ignore-blank-lines: ignore differences on blank lines
  • --ignore-file-name-case: Ignore differences in filename case

Example usage:

  1. Compare the differences of two files, and display the contextual differences (default 3 lines):

    diff file1.txt file2.txt
    
  2. Display file differences using a uniform format:

    diff -u file1.txt file2.txt
    
  3. Compare the differences of two directories (recursive comparison):

    diff -r dir1 dir2
    
  4. Only show whether there is a difference in the file, and do not show the specific content of the difference:

    diff -q file1.txt file2.txt
    

diffThe output format of the command is as follows:

  • >symbol indicates that the line only appears in the second file.
  • <The symbol indicates that the line only appears in the first file.
  • ---marks between different paragraphs of the two documents.
  • ***Marks between identical paragraphs in both documents.

When using diffthe command to compare files, the output shows the differences between the files in text form. Here is an example of the output:

4c4
< This is line 4 in file1.txt.
---
> This is line 4 in file2.txt.

The above output indicates that there is a difference in the fourth line in the two files:

  • <Indicates that the line only appears in the first file (file1.txt).
  • >Indicates that the line only appears in the second file (file2.txt).
  • -The delimiter indicates the difference between two files.
  • The number before the first arrow indicates the line number in the first file.
  • The number after the second arrow indicates the line number in the second file.

In the example above, the fourth line file1.txtwould read "This is line 4 in file1.txt." in the and file2.txtread "This is line 4 in file2.txt." in the .

Guess you like

Origin blog.csdn.net/u012581020/article/details/132432644