Linux diff between two files

How to get the difference (only additions) between two files in linux

I have two files A1 and A2 (unsorted). A1 is previous version of A2 and some lines have been added to A2. How can I get the new lines that are added to A2? Note: I just want the new lines added and dont want the lines which were in A1 but deleted in A2. When i do diff A1 A2 , I get the additions as well as deletions but I want only additions. Please suggest a way to do this.

8 Answers 8

Most of the below is copied directly from @TomOnTime’s serverfault answer here. At the bottom is an attempt that works on unsorted files, but the command sorts the files before giving the diff so in many cases it will not be what is desired. For well-formatted diffs of unsorted files, you might find the other answers more useful (thanks to @Fritz for pointing this out):

Show lines that only exist in file a: (i.e. what was deleted from a)

Show lines that only exist in file b: (i.e. what was added to b)

Show lines that only exist in one file or the other: (but not both)

(Warning: If file a has lines that start with TAB, it (the first TAB) will be removed from the output.)

NOTE: Both files need to be sorted for «comm» to work properly. If they aren’t already sorted, you should sort them:

sort a.sorted sort b.sorted comm -12 a.sorted b.sorted 

If the files are extremely long, this may be quite a burden as it requires an extra copy and therefore twice as much disk space.

Edit: note that the command can be written more concisely using process substitution (thanks to @phk for the comment):

Since we are talking about bash here the last command can be simplified to comm -12 <(sort < a) <(sort < b) using process substitution.

Why does an answer that works only on sorted files have so many upvotes, despite the question clearly stating that the two files are unsorted?

This answer contains the command (see the last one in particular) that works if the files are not sorted. I think it makes sense to give the unsorted commands first since they are easier to understand, and to build up to the commands that do not assume sorted.

@scottkosty: Thanks for your response. Sorry, I didn’t communicate my point clearly in my previous comment. My issue with this approach (in contrast to the diff -based answers) is that the order of the lines is jumbled up by sort, and in many cases, order matters. Imagine comparing two source code files to see which function was added. Only, you sort the source code alphabetically before. Sorting code does not make sense. This is why I think the other answers are better. I think you should at least mention this caveat somewhere in your answer.

Читайте также:  Canon mf4400 ppd linux

Источник

How to Compare Two Files in Linux

If you want to compare two files and decipher the difference, a command called “diff” is used. This guide is focused on providing you the usage of the “diff” command with various options to get the difference between two files.

So, how does the “diff” command actually function? The “diff” command compares the two files and outputs a list of differences between both files. More precisely, it yields a list of modifications that require to be made in the first file to match the second file. The “diff” command is also used by the programmers to get the difference between two source code files to develop patches.

Before diving into the examples, note that the order of the files is very important. Because the “diff” command gives output based on the order of the files.

How to Use “diff” Command in Linux to Compare Files:

The syntax of the “diff” command is mentioned below:

First, create two files. I am creating text files by the name of “test_file_1.txt” and “test_file_2.txt”. These files contain content with a slight difference:

Now use the following command to get the difference:

The standard output displayed the lines that match according to the order of the files mentioned in the command. So, let’s decode the output:

The comparison of the files is labeled, and each label has a number on either side. The format is as follows:

[Line number of file 1][Label(a,c,d)][Line number of file 2]

  • a – Add: Add content in the first file to synch with the second file.
  • c – Change: Indicates that a modification needed in the content of first file to match the second file.
  • d – Delete: Remove content from the first file to match with the second.

“2d1” indicates to delete line number 2 of the first file to match the second file from line number 1.

Similarly, “4c3” means making a change in the fourth line of the first file to match line number 3 of the second file as both lines are slightly different.

There is another method to view the difference, use the “diff” command with the “-y” option:

In the above output, the content of “test_file_1.txt” is displayed on the left side, while the content of “text_file_2.txt” is displayed on the right side. The difference is indicated by the symbols:

The “-W” indicates the width between the content of two files. To get separately and view the difference, use the following:

How to Get Output in One Line Using the “diff” Command:

If the labeled method is hard for you to decode, then there is a simpler approach. Using the “-q” option with the “diff” command gives you output in one line. Well, without any additional information, though:

If the files differ, then the above command will give an output. If the files are identical, then there will be no output. To demonstrate it, I am creating a copy of “test_file_1.txt” using:

A new file will be created by the name of “test_file_3.txt” containing the same content that “test_file_1.txt” has. Now, use:

Since both the files have similar content, therefore, there would be no output.

How to Check the Difference of Files in Context Mode Using the “diff” Command:

To get the comparison in context mode, the “-c” option will be used with the “diff” command:

Читайте также:  Linux echo bin bash

To maintain the difference, the first file is indicated by “***” along with the date and time of the creation, while the second file is indicated by “—”.

The next line signifies a range of lines considered during comparison. For the first file, it is “***1,6****” and for the second file, it is “—1,5—-”:

The difference is indicated by the symbols:

  • + : Line is not present in the first file. Insert it in the first file or remove it from the second file to match both files.
  • – : The line exists in the first file but not in the second file. Try to insert it in the second file or remove it from the first to match both files.
  • ! : Line needs modification in order to match.

How to Check the Difference of Files in Unified Mode Using the “diff” Command:

The unified mode is quite similar to the context mode but without redundant information. The flag we use is “-u”:

In the output, the first file is indicated by “—” and the second by “+++”. The second line shows the number of lines considered for comparison in both files, then the content to be deleted, added, or modified with the symbols with them. There will be no symbol with similar lines in both files.

How to Ignore Case Sensitivity While Using the “diff” Command:

If you want to compare the files while ignoring the case sensitivity, then use the “-i” flag:

For demonstration, I have made the “D” of “Deepin” in the first file small:

As seen in the first command, the difference is indicated; while using “-i” that difference has been removed.

Some other useful options of the “diff” command are listed below:

Option Description
-a This option treats all the files as text files
-B It ignores modification where lines are all blank
-E This option ignores tab expansion
-I It ignores the changes where all lines match
-s Gives output when two files are identical
-w It ignores all white space
-Z It ignores white space at line end

How to Use Vim Editor to Compare Two Files:

Another method to compare two files is using the “vimdiff” command. For that, you need to have vim installed:

Now, to compare two files use:

Both files will be opened side by side. The portion that does not match will be highlighted:

How to Compare Two Files Using “colordiff”:

Another approach is a sort of extension of the “diff” command. You can make the comparison more identifiable by adding colors to it. To install use:

You can replace “diff” with “colordiff” to get the standard output of the “diff” command in colored format.

Conclusion:

To compare files in Linux and even in macOS, a utility used is called the “diff”. The “diff” utility compares two files and gives information about the differences between the two files. The developers primarily use the “diff” command to create patch files.

In this guide, we thoroughly discussed the “diff” command and how to use it to compare two files with different options. We also learned how to use “colordiff” to make the file differences more recognizable. But if you find terminal-based utility hard to use, there are some GUI-based tools as well, such as Kompare, DiffMerge, Meld — Diff Tool and Diffuse — GUI Giff Tool.

Читайте также:  Посмотреть разметку дисков линукс

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.

Источник

Linux diff – How to Compare Two Files and Apply Changes with the Patch Command

Zaira Hira

Zaira Hira

Linux diff – How to Compare Two Files and Apply Changes with the Patch Command

Imagine waking up one day to find out that your production systems are down because of a bug that has yet to be traced. One of your worst nightmares right?

And you also discover that you need to compare code from two versions and the pressure is building to restore the systems. Everyone is panicking and it’s totally legit!

Luckily, there is a Linux utility called diff that has got your back.

What is the diff command in Linux?

Comparing files and finding the differences between them is a widely used operation. This is specially useful when you have to compare complex code or configuration files.

Instead of comparing manually (which has a high chance of human error), Linux gives you a built in and powerful utility called diff . It also saves time.

To compliment diff commands, Linux also provides another command to apply changes from one file to another called patch . In this article, we’ll be looking into these interesting and versatile commands to see how to use them.

diff command syntax

The syntax for diff is shared below:

image-63

In the next example, there are two files that don’t have the same contents. In the output highlighted below, the diff command shows that lines 11 and 14 in showList_v2.js should change to match lines 11 and 13 in showList_v1.js.

image-64

The next way you can use diff is my favorite, as you can see differences side by side.

Just use the -y flag like this:

image-50

The last example I am going to discuss is unified output. This output is often used as input to the patch command. We’ll see how the patch command works as well:

image-67

Below are some other useful flags you can use with diff .

  • -i to ignore case. diff is case sensitive by default.
  • -w to ignore whitespaces in a file. By default whitespaces are considered a difference.

patch Command Syntax

Changes happen all the time in your code, and it is unrealistic and time-consuming to share edited and fixed files for each change. Usually devs share fixes in the code with the team so they are applied instantly.

And using patches is the safest method to distribute improvements only.

Let’s see how patching works:

image-68

However, there is something wrong in the print function and we need a fixture for that. We send the file print_in_js.js to our colleague who fixes the code and sends it back.

First, our colleague is able to find a type in line #3. They correct the file.

Once file is corrected, and the code is functional, they create a patch.

image-69

Once we have the patch, we apply it as follows:

image-72

And yes – our code is fixed!

Wrapping Up

It is relatively simple and straightforward to create and apply patches using patch and diff .

A similar approach works when you’re using version control systems like Git or SVN. Learning the basics really helps you transition to and understand how version control works, which is an important aspect of software development.

Thanks for reading until the end. I would love to connect with you. You can find me here on twitter. Do share your thoughts.

Источник

Оцените статью
Adblock
detector