Linux uses the diff command to compare files.

  

If you need to compare the contents of the system file, then you will use the diff command, you can find the same part between the files, the following small series will give you a detailed introduction Usage of the Linux diff command.

diff command on linux is a very important tool for comparing the contents of the file, in particular, to compare two different versions of files to find the changes in place. Diff prints changes to each line on the command line. The latest version of diff also supports binary files. The output of the diff program is called a patch, because there is a patch program in the Linux system that can update the contents of a.c file to b.c according to the output of diff. Diff is an integral part of version control tools such as svn, cvs, and git.

1. Command format:

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

2. Command function:

The diff command can compare the contents of a single file or directory. If you specify a comparison file, it will only be valid if the input is a text file. Compare the similarities and differences of text files in a line-by-line manner. If you specify a comparison of directories, the diff command compares text files with the same name in both directories. List different binaries, common subdirectories, and files that appear only in one directory.

3. Command Parameters:

- Specifies how many lines of text to display. This parameter must be used in conjunction with the -c or -u parameters.

-a or --text diff presets will only compare text files line by line.

-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 Show all the text and mark the differences.

-C or --context is the same as executing the "-c-”" command.

-d or --minimal Use different algorithms to compare in smaller units.

-D or ifdef The output format of this parameter can be used for the preprocessor macro.

-e or --ed The output format of this parameter can be used for ed script files.

-f or -forward-ed outputs a script format similar to ed, but displays the differences in the order of the original files.

-H or --speed-large-files Speeds up when comparing large files.

-l or --ignore-matching-lines If two files differ in a few lines, and these lines contain both the characters or strings specified in the options, they are not displayed. The difference between the files.

-i or --ignore-case does not check for case differences.

-l or --paginate The result is passed to the pr program for paging.

-n or --rcs The comparison results are displayed in RCS format.

-N or --new-file When comparing directories, if file A only appears in a directory, the default will display: Only in directory: If file A uses the -N parameter, then diff will Compare file A with a blank file.

-p If the file being compared is a C code file, the function name of the difference is displayed.

-P or --unidirectional-new-file is similar to -N, but only when the second directory contains a file that is not in the first directory, this file will be blank with the file. to compare.

-q or --brief only shows the difference, no detailed information is displayed.

-r or --recursive Compare files in subdirectories.

-s or --report-identical-files If no differences are found, the message is still displayed.

-S or --starting-file When comparing directories, start comparisons from the specified file.

-t or --expand-tabs Expands the tab character on output.

-T or --initial-tab Add a tab character in front of each line for alignment.

-u, -U or --unified= Display the difference in file content in a merged manner.

-v or --version displays version information.

-w or --ignore-all-space ignores all space characters.

-W or --width Specify the column width when using the -y parameter.

-x or --exclude does not compare the files or directories specified in the options.

-X or --exclude-from You can save a file or directory type as a text file and then specify this text file in =.

-y or --side-by-side displays the similarities and differences of files in a side-by-side manner.

--help Show help.

--left-column When using the -y parameter, if the contents of one line of the two files are the same, the content of the line is displayed only in the field on the left.

--suppress-common-lines When using the -y parameter, only the differences are shown.

4. Example:

Example 1: Comparing two files

The code is as follows:

[root@localhost test3]# diff log2014.log log2013.log

3c3

2014-03

---

》 2013-03

8c8

2013-07

---

》 2013-08

11,12d10

2013-11

2013-12

Description: The above “3c3” and <8c8” indicates that the log2014.log and log20143log files differ in the 3rd and 8th lines; &ld;;11,12d10” indicates that the first file is the second file. More lines 11 and 12.

There are three tips for the normal display format for diff:

The code is as follows:

a - add

c - change

d - Delete

Instance 2: Side-by-side format output

Command: diff log2013.log log2014.log -y -W 50

Output:

The code is as follows:

[root@localhost test3]# diff log2014.log log2013.log -y -W 50

2013-01 2013-01

2013-02 2013-02< Br>

2014-03 |  2013-03

2013-04 2013-04

2013-05 2013-05

2013-06 2013-06

2013-07 2013- 07

2013-07 |  2013-08

2013-09 2013-09

2013-10 2013-10

2013-11 《

2013-12 《
Previous1234Next page Total 4 pages

Copyright © Windows knowledge All Rights Reserved