Signup/Sign In

Diff Command in Linux

Diff

The diff program compares two files line by line and produces a list of changes. diff compares a copy of standard input to itself as a particular case. "How to use the diff command in Linux" is the topic of this article.

Future Prospects for the "diff" Command

Recognize the differences between one version of a file and another.

Two configuration or application files are compared.

Make a patch file that you may use with the Linux / Unix application patch.

What is the "diff" command, and how does it work?

We have two files named file.txt and file1.txt, for example. As seen below, the data has been entered into file.txt —'

I need to go out and get some apples.

I have to do some laundry.

I need to bathe the dog.

I'm going to have to have the automobile detailed.

The data in file1.txt is as follows:

I need to go out and get some apples.

I've got some laundry to do.

The automobile needs to be washed.

I need to conduct some research on the dog.

To compare the two files, use the diff command as shown below —

$ diff /home/linux/Desktop/file.txt /home/linux/Desktop/file1.txt linux@linux

The output of the preceding command should be as follows:

$ diff /home/linux/Desktop/file.txt /home/linux/Desktop/file1.txt linux@linux
I have to do the laundry.

I need to bathe the dog.

I'm going to have to have the automobile detailed. > —- I've got some laundry to do.

> I'm going to have to wash the automobile.

> I need to acquire the dog's information.

The result's choices should be as follows:

a -I've saved the text to the file.

c -The file has been modified.

d -A deletion operation is carried out.

Lines from the first document

> The second file's lines

According to the output, lines 2 through 4 in the first file must be altered to match lines 2 through 4 in the second file.

Consider another example: two text files should look like this:

file.txt

I'm going to the store.

I need to go out and get some apples.

I'll bathe the dog when I come home.

file1.txt

I'm going to the store.

I need to go out and get some apples.

Oh, and I need to get some shredded cheese as well.

I'll bathe the dog when I come home.

To compare the two files, use the diff command. This is how the command should be written:

/home/linux/Desktop/file.txt /home/linux/Desktop/file1.txt /home/linux/Desktop/file2.txt /home/linux/Desktop/file2.txt /home/linux/Desktop/

The output of the preceding command should be as follows:

2a3

Oh, and I need to get some shredded cheese as well.

2a3 in the result implies "a line has to be inserted after line 2 in the first file: line 3 from the second file."

Congratulations! You now understand "How to use the diff tool in Linux." In our upcoming Linux article, we'll learn more about these commands. Continue reading!



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.