Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Diff command in Linux

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

In view of how to use the Diff command in Linux, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Recommendation of 9 excellent code comparison tools under Linux

These comparison tools are actually based on the diff command, so the usage of some diff commands is described in detail below.

The diff command is a command that analyzes file information and prints out the differences between files. It can compare the contents of two files line by line, or recursively compare the contents of a folder. The output of the diff command lets us know what changes need to be made to make the two files the same, which is very helpful to our work.

Before we begin, let's use the file editor to create two files with the same content. We are using nano here.

$nano diffsample1

Enter the content:

Publish

Of high quality

Technical article

Use Ctrl+X and Y to save and exit.

Create a second file:

$nano diffsample2

Enter the same content and save the exit.

It is important to note that spaces and Tab have an impact on whether the two files are exactly the same.

Use the diff command to see if there are differences between the two files:

$diff diffsample1 diffsample2

At this point, the diff command does not output any information, indicating that the contents of the two files are exactly the same.

Now let's make some changes to diffsample2, change the article to a post, then save and exit.

Then use the diff command:

$diff diffsample1 diffsample2

View the output information:

In the above output, 3c3 indicates that the third line of the first file needs to be replaced with the third line of the second file, and then prompts us what needs to be modified: technical articles and technical posts

The diff command refers not to the first file, but to the second file, and its output information has the following characters:

C: indicates that some changes must be made

A: indicates that something must be added

D: indicates that something must be deleted

The number before the character represents the number of lines in the first file, and the content after the character represents the number of lines in the second file.

< 表示引用的第一个文件中的内容,而 >

Represents the contents of the first file referenced

Now let's try again in the order in which we exchange the two files:

$diff diffsample2 diffsample1

After swapping the file order, the file referenced by diff becomes diffsample1, prompting us to modify the technical post into a technical article.

Now let's edit the diffsample1 to the following:

After deleting the second and third lines, try using the diff command again

$diff diffsample2 diffsample1

Now we can see that because we use diffsample1 as a reference, in order to make the two files the same, we need to delete the second and third lines in diffsample2, and then it will be the same as the first line in diffsample1.

Let's try again the order in which diffsample1 and diffsample2 are exchanged:

$diff diffsample1 diffsample2

As you can see, the prompt given to us by diff is now to add the second and third lines of diffsample2 after the first line in diffsample1.

Now let's test case-sensitive content:

Edit the diffsample1 as follows:

Edit the diffsample1 as follows:

The only difference between the two files is the case of the third line. Try the diff command again.

$diff diffsample1 diffsample2

You can see that the diff command finds the case difference, and if we don't want to check case, we can use the-I option.

$diff-I diffsample1 diffsample2

There is no difference in content at this time, and diff does not output any information.

We can use the-u option to output information in a uniform format:

Use-and + in the output information to indicate what needs to be deleted or added to make the file content the same.

As mentioned earlier, the diff command checks for spaces or tab, and if we want to ignore spaces and tab, we can use the-w option.

Type some spaces and tab after the second line of diffsample2, good, and change the previous uppercase back to lowercase.

Then use the diff command

$diff diffsample1 diffsample2

You can see that you find out the difference, and then use-w to ignore spaces and tab, and there is no output.

This is the answer to the question about how to use the Diff command in Linux. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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

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

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report