One Linux command (+) diff command per day

Source: Internet
Author: User
Tags diff

The diff command compares a given two files in the simplest case. If you use "-" instead of the "file" parameter, the content to be compared will come from standard input. The diff command compares the similarities and differences of text files in a row-by-line manner. If the command specifies a comparison of the directory, the file with the same file name in the directory will be compared without any comparison of its subdirectory files.

(1) Usage:

usage: diff [option parameter] [file 1 or directory 1] [file 2 or directory 2]

(2) function:

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) Option parameters:

1)-y--side-by-side displays the similarities and differences of the files in a parallel manner.

2)-W--width Specifies the width of the column when using the-y parameter.

3)-C display all the text, and mark the difference.

4)-u-u--unified displays the different contents of the file in a merged manner.

5)-R--recursive comparing files in subdirectories

6)-N--rcs Displays the comparison results in RCS format.

(4) Example:

1) [[Email protected] document]# diff t1.txt T2.txt compare two document differences

[email protected] document]# cat >t1.txt <<EOF//The first way to create a new document> This  isA text!> > Name isT1.txt!> the extra content!> I am menangel!>Eof[[email protected] document]# cat>T2.txt//The second way to create a new document This  isA text!Name ist2.txt!^z[2]+ has stopped cat >T2.txt//Press CTRL + Z to stop [[email protected] document]# diff ./document/t1.txt. /document/t2.txt diff Two file parameters can be followed by a relative path or absolute path3, 5c3< Name ist1.txt!< the extra content!< I am Menangel!---> Name ist2.txt![email protected] document]# diff t1.txt T2.txt3, 5c3< Name ist1.txt!< the extra content!< I am Menangel!---> Name ist2.txt!

The first 3 indicates that the 3rd line of two documents is different, the second 5c3 indicates that the first document has 5 rows, and the 2nd document has three rows.

2) [[email protected] document]# diff-y t1.txt t2.txt Compare the differences between two documents

[Email protected] document]# diff-y t1.txt t2.txt This is a text!                             This  is a text!   is t1.txt!                              |     is t2.txt! The extra content!                          <I am Menangel!                              <

3) [[email protected] document]# diff-y-W-t1.txt t2.txt Customize the width of the display on the basis of (2)

 + t1.txt t2.txt  This  is a text!         This  is a text!   is t1.txt!       |     is t2.txt! The extra Conten    <I am Menangel!     <

4) [[email protected] document]# diff-y-W t1.txt t2.txt or T2.txt t1.txt document order effects on results

[Email protected] document]# diff-y-W +t1.txt t2.txt This  isA text! This  isA text!Name ist1.txt!    | Name ist2.txt!The extra Conten<I am Menangel! <[[Email protected] document]# diff-y-w +t2.txt t1.txt This  isA text! This  isA text!Name ist2.txt!    | Name ist1.txt! >The extra Conten> I am menangel!


| 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

5) [[email protected] document]# diff-c t1.txt t2.txt The contents of the two documents to be compared are all displayed to indicate the number of lines, mark the different points

[Email protected] document]# diff-C T1.txt T2.txtT1.txt .- to- -  at: to:25.949100752-0700---t2.txt .- to- -  at: to:54.287100555-0700******************1,5From 1 to 5 rows This  isA text! ! Name ist1.txt! 3rd. 4.5 lines are different!  The extra content! Since t2.txt does not have lines 4th and 5th, the 4th and 5th lines indicate more than T2.txt! I am Menangel!---1,3----//from 1 to 3 rows This  isA text! ! Name ist2.txt! The third row is different


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

(the "+" compares the file with the latter more than the previous line

"-" The latter of the file compared to the previous one is less than the last line)//-u parameter

    “! "Compare files with two different lines

6) [[email protected] document]# diff-u t1.txt t2.txt display different text in a merged way

[Email protected] document]# diff-u t1.txt t2.txt//Its first part is also the basic information of the file---t1.txt .- to- -  at: to:25.949100752-0700//-Indicates the first file+ + T2.txt .- to- -  at: to:54.287100555-0700//+ indicates a second file@@ -1,5+1,3 @@  This  isA text! -name isT1.txt!-the Extra content!-i am menangel!+name ist2.txt! Each minus sign corresponds to a plus sign, and if there is no correspondence it indicates that there is no line in the other file

7) [[Email protected] document]# diff dir1 Dir2 compare two directories

[[ Email protected] document]# mkdir dir1 dir2//Create two directories [[email protected] document]# CD Dir1[[email protected] dir1]# cat>text1 <<EOF//Create TEXT1,TEXT2 in Dir1 to create text1,text3 in Dir2>Dir:dir1>Name:text1> > Total4!>Eof[[email protected] dir1]# cat>text2 <<EOF> I am menangel!> I am studying the order of linux!>Eof[[email protected] dir1]# CD ./Dir2[[email protected] dir2]# cat>text1 <<EOF>Dir:dir2>Name:text1> > > Total5!>Eof[[email protected] dir2]# cat>text3 <<EOF> Working hard makes success!> I am menangel!>Eof[[email protected] dir2]# CD ./[[Email protected] document]# diff dir1 Dir2 only exist in Dir2: Text3diff dir1/text1 dir2/Text1//encountering files with the same name automatically compare 1C1<Dir:dir1--->Dir:dir24c4,5< total4!---> > Total5!exist only in Dir1: Text2

8) [[Email protected] document]# diff dir1 Dir2 >dir.log generate patches, in fact, to output to the standard output of the content output to a custom file

[[Email protected] document]# diff dir1 dir2 >dir.log[[email protected] document]# cat Dir.log exists only in Dir2: Textdiff D Ir1/text1 dir2/text11c1< dir:      dir1---> dir:      dir24c4,54  5! exist only in Dir1: Text2

9) [[Email protected] document]# diff t1.txt T2.txt>t12.log generate patches and update files with this patch

[email protected] document]# cat t1.txt t2.txt This  isA text!Name ist1.txt!The extra content!I am Menangel! This is T1.txt's content. This  isA text!Name ist2.txt! This is T2.txt's content.[email protected] document]# diff t1.txt T2.txt>t12.log//Generate patches [[email protected] document]# patch t1.txt t12.log//Patch the T1.txt to make its contents t2.txt content Patchi ng file T1.txt[email protected] document]# cat T1.txt This  isA text!Name ist2.txt![[Email protected] document]# patch T2.txt t12.log//to T2.txt patching, but it seems a little different patching file t2.txtreversed (or P reviously applied) Patch detected! Assume-r?[n] Y//Don't know why? [email protected] document]# cat T2.txt This  isA text!Name ist1.txt!The extra content!I am Menangel!

(5) Other:

Extended Knowledge:

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.

One Linux command (+) diff command per day

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: 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.