Linux diff command

Source: Internet
Author: User
Tags diff

General normal comparison of two files with Vimdiff, is directly into the Vim interface, if the comparison of two folders under the file, you can use diff, note, here will only compare folders under the same name file, he will list the different points.

See how to use the diff command under Linux

Diff

The function of the diff command in Linux is to compare two text files on a line-by-row basis, listing their differences. It checks the given file for a system and displays all the different rows in the two files without requiring the file to be sorted beforehand.

1 "diff command function

The function of the diff command in Linux is to compare two text files on a line-by-row basis, listing their differences. It checks the given file for a system and displays all the different rows in the two files without requiring the file to be sorted beforehand.

2 "Grammar

diff [Options] file1 file2

The command tells the user which rows to modify in order to make the two files File1 and file2 consistent. If File1 or file2 is represented by "-", the standard input is indicated. If File1 or file2 is a directory, diff will use a file of the same name in that directory for comparison.

3 [options] main parameters

-A: treats all files as text files.

-B or –ignore-space-change ignores the difference caused by whitespace.

-B or –ignore-blank-lines ignores the difference caused by empty rows.

-C: Use the schema output format.

-H: Use heuristics to accelerate searches of large files.

-I: Ignores case changes.

-N–RCS: Output RCS format.

-N or –new-file when the directory is compared, if file a only appears in a directory, www.linuxidc.com will display: only in the directory; If file a uses the-n parameter, diff will compare file A to a blank file.

-R or –recursive to compare files in subdirectories.

-u,-u the number of columns or –unified= the number of columns to display the different contents of the file in a merged manner.

4 examples of how to use the method

For example:

diff /usr/xu mine

Compare the file named mine in the directory/usr/xu with the mine file in the current directory.

Typically the output is made up of lines in the following form:

N1 a N3,n4

N1,n2 D N3

N1,N2 C N3,N4 These lines like the ed command to convert Filel into file2. The line numbers (N1,N2) before the letters (a, D, and C) are for file1, and the following line numbers (N3,N4) are for file2. The letters A, D, and C represent additional, delete, and modify actions, respectively.

After each line in the above form follows a number of rows that are affected, the line that begins with "" is the first file, and the line that begins with "" is the second file.

Diff can distinguish between block and character device files and FIFO (pipeline files), and does not compare them to normal files.

If both File1 and file2 are directories, diff generates a lot of information.

5 "diff most commonly used features

Diff has a lot of features that we don't usually use, and the most common feature is the ability to generate patch files:

Diff-urn old/new/"Mysoft.patch

The parameter-u means using the unified format,-R for the comparison directory, and-N to treat the nonexistent file as an empty file, so that the newly added file will also appear in the patch file.

Then use the following commands where you need to apply patches:

Patch-p0 "Mysoft.patch

  The-y command of diff

(long format –side-by-side) You can divide the screen into left and right parts to compare the differences between the two files . Many graphical comparison tools have this functionality, but this is useful if you can use only the command line. If you want to change the width of the left and right parts, you can specify them by the-W (–width) parameter.

The –ignore-blank-lines parameter can not check for blank lines. In this case, the DOS format and UNIX-formatted files are compared to each other, so there is not a large number of differences because of inconsistent line breaks.

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.