In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains how Linux uses the diff command to compare files to find out the same parts between files. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how Linux uses the diff command to compare files to find out the same parts between files.
The diff command is a very important tool on linux to compare the contents of files, especially two different versions of files to find changes. Diff prints changes to each line on the command line. The latest version of diff also supports binaries. The output of the diff program is called a patch, because there is also a patch program in the Linux system that updates the file contents of A.C to B.C based on the output of diff. Diff is an integral part of version control tools such as svn, cvs, git, and so on.
1. Command format:
Diff [parameters] [file 1 or directory 1] [file 2 or directory 2]
2. Command function:
The diff command compares the contents of a single file or directory. If you specify that you are comparing a file, it is valid only if you enter it as a text file. Compare the similarities and differences of text files on a line-by-line basis. If you specify a directory to compare, the diff command compares text files with the same name in the two directories. Lists different binaries, common subdirectories, and files that appear in only one directory.
3. Command parameters:
-specifies how many lines of text to display. This parameter must be used with the-c or-u parameter.
The-an or-- text diff preset only compares text files line by line.
-b or-- ignore-space-change do not check for differences in space characters.
-B or-- ignore-blank-lines does not check for blank lines.
-c shows all the text and marks the differences.
-C or-- context is the same as executing the "- c -" instruction.
-d or-- minimal use different algorithms to make comparisons in smaller units.
-D or ifdef the output format of this parameter can be used for preprocessor macros.
The output format of-e or-- ed this parameter can be used in the script file of ed.
The format of the-f or-forward-ed output is similar to ed's script file, but the differences are shown in the order of the original file.
-H or-- speed-large-files can speed up when you compare large files.
-l or-- ignore-matching-lines if two files are different on certain lines, and both lines contain the characters or strings specified in the option, the difference between the two files is not displayed.
-I or-- ignore-case does not check for case differences.
-l or-- paginate passes the results to the pr program for paging.
-n or-- rcs will display the comparison results in RCS format.
When comparing directories with-N or-- new-file, if file An appears only in a directory, the default will display: Only in directory: if file A uses the-N parameter, diff will compare file A with a blank file.
-p if the compared file is a C language program code file, the name of the function where the difference is located is displayed.
-P or-- unidirectional-new-file is similar to-N, but only if the second directory contains a file that is not available in the first directory will this file be compared to a blank file.
-Q or-- brief only shows whether there is a difference, not detailed information.
-r or-- recursive compare files in a subdirectory.
-s or-- report-identical-files still displays information if it does not find any difference.
When comparing directories,-S or-- starting-file starts the comparison from the specified file.
-t or-- expand-tabs expands the tab character on output.
-T or-- initial-tab precede each line with a tab character for alignment.
-UMAL or-- unified= show differences in the contents of the file in a merged way.
-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 do not compare 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 this text file in =.
-y or-- side-by-side shows the similarities and differences of the file in a side-by-side manner.
-- help displays help.
When left-column uses the-y parameter, if one line of the two files is the same, it only displays that line in the field on the left.
-- suppress-common-lines shows only the difference when using the-y parameter.
4. Use an example:
Example 1: compare 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
Note: the words "3c3" and "8c8" above indicate that the contents of the log2014.log and log20143log files are different on lines 3 and 8; "11J 12d10" indicates that the first file has 11 and 12 lines more than the second file.
There are three prompts for diff's normal display format:
The code is as follows:
A-add
C-change
D-delete
Example 2: output in side-by-side format
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
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 "
[root@localhost 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 two files are different.
"" means that the latter file has one line less than the previous file.
"" means that the latter file has one more line than the previous file.
Example 3: context output format
Command: diff log2013.log log2014.log-c
Output:
The code is as follows:
[root@localhost test3] # diff log2013.log log2014.log-c
* log2013.log 2012-12-07 16 purl 36V 26.000000000 + 0800
-log2014.log 2012-12-07 18 01V 54.000000000 + 0800
***************
* 1Jing 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 [root@localhost test3] # diff log2014.log log2013.log-c
* log2014.log 2012-12-07 18 purl 01V 54.00000000000 + 0800
-log2013.log 2012-12-07 16 purl 36V 26.000000000 + 0800
***************
* 1Jing 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 [root@localhost test3] #
Description:
This method compares the file in the first two lines, where there are three special characters:
The latter of the file compared with "+" is one line more than the previous one.
The latter of the "-" comparison file is one line less than the previous one.
"!" There are different lines between the two files.
Example 4: output in uniform format
Command: diff log2014.log log2013.log-u
Output:
The code is as follows:
[root@localhost test3] # diff log2014.log log2013.log-u
-log2014.log 2012-12-07 18 01V 54.000000000 + 0800
+ log2013.log 2012-12-07 16 purl 36V 26.000000000 + 0800
@ @-1BI 12 + 1BI 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 01V 54.000000000 + 0800
+ log2013.log 2012-12-07 16 purl 36V 26.000000000 + 0800
"-" indicates the document before the change, and "+" indicates the document after the change.
In the second part, the position of the change begins and ends with two @.
@ @-1BI 12 + 1BI 10 @ @
The preceding "- 1 log2014.log 12" is divided into three parts: the minus sign indicates the first file (that is, the file), "1" represents the first line, and "12" represents 12 consecutive lines. Taken together, it means that the following is the 12 consecutive lines of the first file starting at line 1. Similarly, "+ 1re10" means that after the change, it becomes the second file with 10 consecutive lines starting from line 1.
Example 5: compare different folders
Command: diff test3 test6
Output:
The code is as follows:
[root@localhost 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
[root@localhost test] #
Example 6: compare two different files and produce patches
Command: diff-ruN log2013.log log2014.log "patch.log
Output:
The code is as follows:
[root@localhost test3] # diff-ruN log2013.log log2014.log "patch.log
[root@localhost test3] # ll
Total 12
-rw-r--r-- 2 root root 80 12-07 16:36 log2013.log
-rw-r--r-- 1 root root 96 12-07 18:01 log2014.log
-rw-r--r-- 1 root root 24812-07 21:33 patch.log
[root@localhost test3] # cat patc.log
Cat: patc.log: there is no such file or directory
[root@localhost test3] # cat patch.log
-log2013.log 2012-12-07 16 purl 36V 26.000000000 + 0800
+ log2014.log 2012-12-07 18 purl 01purl 54.000000000 + 0800
@ @-1BI 10 + 1J 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 [root@localhost test3] #
Example 7: patch
Command: cat log2013.log
Output:
The code is as follows:
[root@localhost test3] # cat log2013.log
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10 [root@localhost test3] # patch log2013.log patch.log
Patching file log2013.log
[root@localhost test3] #
[root@localhost 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 [root@localhost test3] #
At this point, I believe you have a deeper understanding of "how Linux uses the diff command to compare files to find out the same parts between files". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 280
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.