The diff command Usage tutorial for contrasting files under Linux

Source: Internet
Author: User
Tags diff

General overview

diff [Options] source file destination file

Describe

In the simplest case, diff compares the contents of two files (source and destination files). The file name can be-text read by the standard input device. As a special case, diff--Compare a standard input to its own copy if the source file is a directory and the destination file is not (a directory), diff compares the files in the source file (directory) with the same name as the destination file, and vice versa. Non-directory files cannot be-. If both the source and destination files are directories, diff compares the corresponding files in the two directories, sorted alphabetically; this comparison is not recursive unless given-R or--recursive. Diff does not look at the contents of a directory as it is a file to compare. The specified file cannot be a standard input because the standard input is nameless and the idea of "files with the same name" does not apply. Diff option by-, start so normal source file (name) and destination file (name) can not be used-start. However,--it can be considered reserved even as the beginning of the filename (they begin with-.)

Options

The following is a summary of all of the diff options accepted by GNU. Most of the options have two identical names, one is a single followed-back letter, and the other is a long name drawn from it. Multiple single letter options (unless they are ambiguous) the command-line syntax that can be combined into a single line-ac is equivalent to-a-c. Long named options can be shortened to any unique prefix of their name. Use ([and]) to enclose options that show options ambiguous

-Number of rows (an integer)

Displays the number of context rows (an integer). This option does not specify the output format itself, which is not effective unless used in combination with-C or-U. This is the abandoned option, and the context must have at least two lines for the correct operation.

-A

All files are treated as text files and are compared to each other, even if they do not appear to be text files.

-B

Ignores changes caused by spaces.

-B

Ignores the changes caused by inserting the delete blank line.

--brief

Only report whether the file is different and care about the details of the difference.

-C

Use the context output format.

-C line Count (an integer)

--context[=lines]

Displays the specified number of rows (an integer), or Sankong (when the number of rows is not given), using the context output format. For the correct operation, the context must have at least two lines.

--changed-group-format=format

Use format to output a set of rows that contain two different files, in the form of If-then-else.

-D

Changing the algorithm may find a smaller set of changes. This makes the diff slower (sometimes slower).

-D Name

Merges If-then-else format output, preprocessing macros (provided by the name parameter) condition.

-E

--ed

The output is a valid ed script.

--exclude=pattern

When comparing directories, ignore and match pattern (style) in the table of contents.

--exclude-from=file

When comparing directories, ignore the files and directories in the directory that match any style contained in the file.

--expand-tabs

To protect the tab alignment of input files by extending tab to spaces at output

-F

Produces a very much like Ed script output, but the order in which they appear in the file changes

-F RegExp

In context and in a unified format, a matching regexp is displayed for each chunk of the difference. Some of the preceding lines.

--forward-ed

Produces output like Ed scripts, but they change in the order in which files appear.

-H

This option does not work now, and it presents UNIX compatibility.

-H

Use heuristic rules to accelerate the operation of large files that have many discrete small differences.

--horizon-lines=lines

Compares the last line with a common prefix for a given number of rows, and the most forward with a common or suffix.

-I.

Ignores case.

-I. regexp

Ignores the changes caused by inserting, deleting rows (provided by the RegExp parameter reference).

--ifdef=name

Merges If-then-else format output, preprocessing macros (provided by the name parameter) condition.

--ignore-all-space

Ignores whitespace when comparing rows.

--ignore-blank-lines

Ignore insert and delete blank lines

--ignore-case

Ignores case.

--ignore-matching-lines=regexp

The insert Delete Row is ignored (referenced by the regexp parameter).

--ignore-space-change

Ignores the amount of white space.

--initial-tab

Output the TAB key instead of a space before the line of text (whether it is a regular or formatted contextual relationship). This is caused by the fact that the tab alignment appears to be normal.

-L

Produces output that is encoded through PR.

-L Label

--label=label

The characters given with the label are used in place of the file header instead of the filename output.

--left-column

Print the left side of two public lines in a parallel way

--line-format=format

Use format to output all rows in the If-then-else format.

--minimal

Changing the algorithm may find a smaller set of changes. This makes the diff slower (sometimes slower).

-N

Output rc-format diffs; Except that the number of rows specified by each instruction is affected as f.

-N

--new-file

In a directory comparison, if the file is found in only one of the directories, it is considered to be an empty file in another directory.

--new-group-format=format

Use format to output a row group that is only fetched in the second file in if-then-else format

--new-line-format=format

Use format to output only one row in the second file in if-then-else format

--old-group-format=format

Use format to output a row group that is only fetched in the first file in if-then-else format

--old-line-format=format

Use format to output only one row in the first file using format in if-then-else format

-P

Shows a change with a C function.

-P

In a directory comparison, if the file is found in only one of the directories, it is considered to be an empty file in another directory.

--paginate

Produces output that is encoded through PR.

-Q

Reports only whether the file is different and does not report detailed differences.

-R

When comparing directories, recursively compares any subdirectories found.

--rcs

Output rc-format diffs; Except that the number of rows specified by each instruction is affected as f.

--recursive

When comparing directories, recursively compares any subdirectories found.

--report-identical-files

-S

Report two files the same.

-S file

When the catalog is compared, the file begins. This is used to continue the interrupted comparison.

--sdiff-merge-assist

Print additional information to help the Sdiff. Sdiff use these options when running diff. These options are not intended for use directly by users.

--show-c-function

Shows a change with a C function.

--show-function-line=regexp

In context and in a uniform format, for each chunk of the difference, a matching regexp is shown. Some of the preceding lines

--side-by-side

Use the parallel output format.

--speed-large-files

Use heuristic rules to accelerate the operation of large files that have many discrete small differences.

--starting-file=file

When the catalog is compared, the file begins. This is used to continue the interrupted comparison.

--suppress-common-lines

Public lines are not printed in a side-by-side format.

-T

To protect the tab alignment of input files by extending tab to spaces at output

-T

Output tab to replace spaces before lines of text, whether regular or formatted contextual relationships. The reason for this is that the tab alignment appears to be normal.

--text

All files are treated as text files and are compared to each other, even if they do not appear to be text files.

-U

Use a uniform output format.

--unchanged-group-format=format

Use format to output a common row group of two files, in the format of If-then-else.

--unchanged-line-format=format

Use format to output a public row of two files in the format of If-then-else.

--unidirectional-new-file

In a directory comparison, if the file is found in only one of the directories, it is considered to be an empty file in another directory.

-U lines

--unified[=lines]

Output using the Pre-and Sankong format, displaying the specified number of rows (an integer), or the (when the number of rows is not given). For the correct operation, the context must have at least two lines.

-V

--version

Output diff version number.

-W

Ignore spaces when comparing rows

-W Columns

--width=columns

The specified column width is used when the output is in parallel format.

-X pattern

When comparing directories, ignore and match pattern (style) in the table of contents.

-X File

When comparing directories, ignore the files and directories in the directory that match any style contained in the file.

-Y

Use side-by-side format output

Instance:

Suppose we have two files (File1 and file2):

The code is as follows:

$ cat File1

Hi

Hello,

How are?

I am Fine,

Thank you.

The code is as follows:

$ cat File2

Hello,

Hi

How are?

I am fine.

You can see that two files are a little different. Now, let's see how the diff command finds the difference between the two.

Run the diff command like this:

The code is as follows:

$ diff file1 File2

1d0

< Hi,

2a2

> Hi,

4,5c4

< I am Fine,

< Thank you.

---

> I am Fine.

You can see the diff followed by the names of two files as arguments to the command line, and it generates a diff comparison in the output. The output is not easy to understand. The reason is that it was used by computers rather than for humans. However, let's decode the output one step at a while:

Note – in the following text, File1 and file2 will be treated as old files and new files.

The code is as follows:

1d0

< Hi,

Here, 1d0 this line means that the first line of the old file should be deleted (d) to synchronize the first line of the two files. The lines in the old file that need to be deleted are marked ' < '.

The code is as follows:

2a2

> Hi,

Here, the 2a2 line means that the second line in the new file should be added to the second line of the old file. The rows to be added are marked with ' > ' on the next line in the output.

The code is as follows:

4,5c4

< I am Fine,

< Thank you.

---

> I am Fine.

Here, 4,5c4 this line means that 4 to 5 lines in the old file are now changed and need to be replaced with line 4th in the new file. The rows added and deleted are indicated by ' > ' and ' < ' respectively.

So, to sum up,

First parameter of the diff command is treated as an old file and the second parameter is treated as a new file.

Such expressions as 1d0, 2A2, 4,5C4 can be decoded with syntax to [the line number of the old file or the range of the line] [behavior] [the line number of the new file or the range of lines]. The ' behavior ' here can be append, delete, or change the substitution.

' < ' represents the deleted row, while ' > ' represents the added row.

In addition to files, the diff command can compare two directories. Let's learn from an example.

Here is what the ' New_dir ' directory contains:

The code is as follows:

$ ls new_dir/

File file2 frnd frnd1.cpp log1.txt log3.txt log5.txt

File1 file name with spaces Frnd1 Frnd.cpp log2.txt log4.txt

This is the contents of the ' Orig_dir ' directory: The original and here are the contents of a directory named ' Olddir ', where ' olddir ' should be a clerical error. )

The code is as follows:

$ ls orig_dir/

File1 file name with spaces Frnd1 frnd.cpp log2.txt log4.txt test

File2 frnd frnd1.cpp log1.txt log3.txt log5.txt

The following is the output of the diff command after execution:

The code is as follows:

$ diff new_dir/orig_dir/

Only in new_dir/: file

Only in orig_dir/: Test

You can see that when the diff command is used to compare the two directories, it is easy to show the missing files in the two files.

Here are some of the options that are commonly used at the command line:

1. Ignore case with-I

If two files contain the same text but have different capitalization, the diff command will still report it differently by default.

Like what:

The code is as follows:

$ cat File1

Hi

$ cat File2

Hi

$ diff file1 File2

1c1

< HI

---

> Hi

You can see the diff command reported a different case in the output.

To remove this default behavior, use the-i option.

Here's an example:

The code is as follows:

$ diff-i file1 file2

This way you can see that no output is generated, which is the default behavior when two files are the same.

2. Report two files the same with the-s option

At the back of example 1, we see that if the file is the same diff, the report will not be generated. Although this default behavior is good, it can still cause a lot of confusion, especially for beginners. So if you have a decent diff command to explicitly report two different files, then use the-s command option.

To give an example:

The code is as follows:

$ diff-is file1 file2

Files file1 and File2 are identical

You can see I added the-s option in the following example, this diff command will explicitly report that two files are the same.

3. Ignore spaces with-B

Another common use is that the diff report file has a different number of spaces.

An example is provided:

The code is as follows:

$ cat File1

Hi, how are are you?

$ cat File2

Hi, how are are you?

The only difference in observing these two files is the extra space between ' are ' and ' you ' in file2. Now, when you compare two files using the diff command, the output is as follows:

The code is as follows:

$ diff file1 File2

1c1

< Hi, how are are you?

---

> Hi, how are are you?

So you can see the diff command reporting different. But if you want to ignore these spaces, use the-B option.

The code is as follows:

$ diff-b file1 file2

This way you can see that because of the-B option, the diff command reports that the two files are the same.

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.