Linux diff command

Source: Internet
Author: User
Tags diff

The diff command is a very important tool on Linux to compare the contents of a file, especially to compare two different versions of files to find the changes. Diff prints each line's changes on the command line. The latest version of diff also supports binary files. The output of the diff program is called Patch, because there is also a patch program in the Linux system that updates the contents of the A.C file to B.C based on the diff output. Diff is an integral part of version control tools like 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 individual files or directory contents. If you specify that the comparison is a file, it is only valid if the input is a text file. Compare the similarities and differences of text files in a row-by-line manner. If you specify a table of contents, the diff command compares a text file with the same name in two directories. Lists the different binaries, common subdirectories, and files that appear only in one directory.

3. Command parameters:

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

The-A or--text diff preset will only compare text files line by row.

-B or--ignore-space-change do not check for differences in whitespace characters.

-B or--ignore-blank-lines does not check for blank lines.

-C Displays all the text and marks the difference.

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

-D or--minimal use different algorithms to compare in smaller units.

-D or ifdef the output format of this parameter is available for the predecessor Processor macro.

-E or--ed the output format of this parameter is available for the ed script file.

The-f or-forward-ed output is formatted like an ED script file, but is displayed differently in the order of the original files.

-h or--speed-large-files can speed up when comparing large files.

-L or--ignore-matching-lines if two files are different in a few lines, and these lines both contain the characters or strings specified in the option, the differences between the two files are not displayed.

-I or--ignore-case do not check for differences in case.

-L or--paginate 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 only appears in a directory, the preset will display: Only in directory: file A If you use the-n parameter, diff will compare file A with a blank file.

-P If the file is compared to a C language code file, the name of the function where the difference is displayed.

-P or--unidirectional-new-file is similar to-N, but this file is compared to a blank file only if the second directory contains a file that is not in the first directory.

-Q or--brief display only if there is no difference, no detailed information is displayed.

-R or--recursive to 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 comparing from the specified file.

-T or--expand-tabs expands the tab character when it is output.

-T or--initial-tab the tab character before each line to align.

-u,-u or--unified= displays the different contents of the file in a merged manner.

-V or--version displays version information.

-W or--ignore-all-space ignores all whitespace characters.

-W or--width specifies 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 the file or directory type as a text file, and then specify the text file in =.

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

--help display Help.

--left-column when using the-y parameter, if the contents of one row of two files are the same, the contents of that row are displayed only in the field on the left.

--suppress-common-lines displays only the differences when using the-y parameter.

4. Usage examples:

Example 1: Comparison of two files

Command:

Output:

[Email protected] test3]# diff Log2014.log Log2013.log

3c3

< 2014-03

---

> 2013-03

8c8

< 2013-07

---

> 2013-08

11,12d10

< 2013-11

< 2013-12

Description

The "3c3" and "8c8" above indicate that Log2014.log and Log20143log files differ in rows 3 and 8th, and "11,12D10" means that the first file is 11th and 12 rows longer than the second file.

The normal display format for diff has three tips:

A-add

C-change

D-delete

Example 2: Side-by format output

Command:

Diff Log2013.log Log2014.log-y-W 50

Output:

[[Email protected] test3]# diff Log2014.log Log2013.log-y-W 50

2013-01 2013-01

2013-02 2013-02

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 <

[[Email protected] test3]# diff Log2013.log Log2014.log-y-W 50

2013-01 2013-01

2013-02 2013-02

2013-03 | 2014-03

2013-04 2013-04

2013-05 2013-05

2013-06 2013-06

2013-07 2013-07

2013-08 | 2013-07

2013-09 2013-09

2013-10 2013-10

> 2013-11

> 2013-12

Description

| Indicates that the contents of the front and back 2 files are different

"<" indicates that the following file is 1 lines less than the previous file

">" indicates that the following file has 1 lines more than the previous file

Example 3: Context output format

Command:

Diff Log2013.log Log2014.log-c

Output:

[Email protected] test3]# diff Log2013.log Log2014.log-c

Log2013.log 2012-12-07 16:36:26.000000000 +0800

---log2014.log 2012-12-07 18:01:54.000000000 +0800

***************

1,10 * * *

2013-01

2013-02

! 2013-03

2013-04

2013-05

2013-06

2013-07

! 2013-08

2013-09

2013-10

---1,12----

2013-01

2013-02

! 2014-03

2013-04

2013-05

2013-06

2013-07

! 2013-07

2013-09

2013-10

+ 2013-11

+ 2013-12[[email protected] test3]# diff Log2014.log Log2013.log-c

Log2014.log 2012-12-07 18:01:54.000000000 +0800

---log2013.log 2012-12-07 16:36:26.000000000 +0800

***************

1,12 * * *

2013-01

2013-02

! 2014-03

2013-04

2013-05

2013-06

2013-07

! 2013-07

2013-09

2013-10

-2013-11

-2013-12

---1,10----

2013-01

2013-02

! 2013-03

2013-04

2013-05

2013-06

2013-07

! 2013-08

2013-09

2013-10[[email protected] test3]#

Description

In this way, a description of the comparison file is made in the first two lines, here are three special characters:

The "+" comparison of the latter of the files is more than the previous line

"-" compares the latter of the file with a smaller line than the previous

“! "Compare files with two different lines

Example 4: Unified format output

Command:

Diff Log2014.log Log2013.log-u

Output:

[Email protected] test3]# diff Log2014.log Log2013.log-u

---log2014.log 2012-12-07 18:01:54.000000000 +0800

+ + + log2013.log 2012-12-07 16:36:26.000000000 +0800

@@ -1,12 +1,10 @@

2013-01

2013-02

-2014-03

+2013-03

2013-04

2013-05

2013-06

2013-07

-2013-07

+2013-08

2013-09

2013-10

-2013-11

-2013-12

Description

The first part of it is also the basic information of the file:

---log2014.log 2012-12-07 18:01:54.000000000 +0800

+ + + log2013.log 2012-12-07 16:36:26.000000000 +0800

"---" means the document before the change, and "+ + +" means the document after the change.

The second part, the position of the change with two @ as the first and end.

@@ -1,12 +1,10 @@

The preceding " -1,12" is divided into three parts: the minus sign indicates the first file (that is, Log2014.log), "1" means line 1th, and "12" means 12 rows in a row. Together, it means that the following is the first file, starting from line 1th, 12 consecutive lines. Similarly, "+1,10" means that after a change, the second file becomes a row of 10 lines starting from line 1th.

Example 5: Compare folders differently

Command:

Diff Test3 Test6

Output:

[Email protected] test]# diff test3 Test6

Only in Test6:linklog.log

Only in Test6:log2012.log

Diff Test3/log2013.log Test6/log2013.log

1,10c1,3

< 2013-01

< 2013-02

< 2013-03

< 2013-04

< 2013-05

< 2013-06

< 2013-07

< 2013-08

< 2013-09

< 2013-10

---

> hostnamebaidu=baidu.com

> hostnamesina=sina.com

> Hostnames=true

Diff Test3/log2014.log Test6/log2014.log

1,12d0

< 2013-01

< 2013-02

< 2014-03

< 2013-04

< 2013-05

< 2013-06

< 2013-07

< 2013-07

< 2013-09

< 2013-10

< 2013-11

< 2013-12

Only in Test6:log2015.log

Only in Test6:log2016.log

Only in Test6:log2017.log

[Email protected] test]#

Description

Example 6: Compare two different files and produce patches

Command:

Diff-run Log2013.log Log2014.log >patch.log

Output:

[Email protected] test3]# diff-run log2013.log log2014.log >patch.log

[email protected] test3]# LL

Total 12

-rw-r--r--2 root root 12-07 16:36 Log2013.log

-rw-r--r--1 root root 12-07 18:01 Log2014.log

-rw-r--r--1 root root 248 12-07 21:33 Patch.log

[email protected] test3]# cat Patc.log

Cat:patc.log: No file or directory

[email protected] test3]# cat Patch.log

---log2013.log 2012-12-07 16:36:26.000000000 +0800

+ + + log2014.log 2012-12-07 18:01:54.000000000 +0800

@@ -1,10 +1,12 @@

2013-01

2013-02

-2013-03

+2014-03

2013-04

2013-05

2013-06

2013-07

-2013-08

+2013-07

2013-09

2013-10

+2013-11

+2013-12[[email protected] test3]#

Description

Example 7: Patching

Command:

Output:

[email protected] test3]# cat Log2013.log

2013-01

2013-02

2013-03

2013-04

2013-05

2013-06

2013-07

2013-08

2013-09

2013-10[[email protected] test3]# patch Log2013.log Patch.log

Patching file Log2013.log

[Email protected] test3]#

[email protected] test3]# cat Log2013.log

2013-01

2013-02

2014-03

2013-04

2013-05

2013-06

2013-07

2013-07

2013-09

2013-10

2013-11

2013-12[[email protected] test3]#

Description

Linux diff command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.