Linux difference between 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.

7 Answers 7

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.

Читайте также:  Линукс вывести текущее время

Источник

9 Best File Comparison and Difference (Diff) Tools for Linux

While writing program files or normal text files, programmers and writers sometimes want to know the difference between two files or two versions of the same file. When you compare two computer files on Linux, the difference between their contents is called a diff. This description was born out of a reference to the output of diff, the well known Unix command-line file comparison utility.

9 Best Linux File Diff or Comparison Tools

There are several file comparison tools that you can use on Linux, and in this review, we shall look at some of the best terminal based and GUI diff tools you can take advantage of while writing code or other text files.

1. diff Command

I like to start with the original Unix command-line tool that shows you the difference between two computer files. Diff is simple and easy to use, it comes pre-installed on most Linux distributions. It compares files line by line and outputs the difference between them.

You can check out the manual entry for diff to easily use it.

Linux diff Command to Compare Files

There are some wrappers for the diff tool that enhance its functionality and these include:

colordiff Command

Colordiff is a Perl script that produces same output as diff, but with color and syntax highlighting. It has customizable color schemes.

You can install Colordiff on your Linux systems, using default package manager tools called yum, dnf or apt-get as shown.

# yum install colordiff [On CentOS/RHEL/Fedora] # dnf install colordiff [On Fedora 23+ version] $ sudo apt-get install colordiff [On Debian/Ubuntu/Mint]

You can check out the manual entry for Colordiff as shown.

Linux Colordiff Command - Color diff Output

wdiff Command

The wdiff utility is a front end to diff command used to compare files on a word by word basis. This program is very useful when comparing two texts for changed words and for which paragraphs have been refilled.

To install wdiff on your Linux systems, run:

# yum install wdiff [On CentOS/RHEL/Fedora] # dnf install wdiff [On Fedora 23+ version] $ sudo apt-get install wdiff [On Debian/Ubuntu/Mint]

Use wdiff manual for how to use it on Linux.

wdiff Compare Two Files in Linux

2. Vimdiff Command

Vimdiff works in an advanced manner in comparison to diff utility. It enables a user to edit up to four versions of a file while showing their differences. When you run it, Vimdiff opens two or three or four files using vim text editor.

Vimdiff Edit Multiple Files for Differences

Having looked the old school diff tools, lets quickly move to some GUI diff tools available on Linux.

3. Kompare

Kompare is a diff GUI wrapper that allows users to view differences between files and also merge them.

Some of its features include:

Kompare Tool - Compare Two Files in Linux

  1. Supports multiple diff formats
  2. Supports comparison of directories
  3. Supports reading diff files
  4. Customizable interface
  5. Creating and applying patches to source files

4. DiffMerge

DiffMerge is a cross-platform GUI application for comparing and merging files. It has two functionality engines, the Diff engine which shows the difference between two files, which supports intra-line highlighting and editing and a Merge engine which outputs the changed lines between three files.

It has got the following features:

Читайте также:  Шрифты libreoffice linux mint

DiffMerge - Compare Files in Linux

  1. Supports directory comparison
  2. File browser integration
  3. Highly configurable

5. Meld – Diff Tool

Meld is a lightweight GUI diff and merge tool. It enables users to compare files, directories plus version controlled programs. Built specifically for developers, it comes with the following features:

Meld - A Diff Tool to Compare File in Linux

  1. Two-way and three-way comparison of files and directories
  2. Update of file comparison as a users types more words
  3. Makes merges easier using auto-merge mode and actions on changed blocks
  4. Easy comparisons using visualizations
  5. Supports Git, Mercurial, Subversion, Bazaar plus many more

6. Diffuse – GUI Diff Tool

Diffuse is another popular, free, small and simple GUI diff and merge tool that you can use on Linux. Written in Python, It offers two major functionalities, that is: file comparison and version control, allowing file editing, merging of files and also output the difference between files.

You can view a comparison summary, select lines of text in files using a mouse pointer, match lines in adjacent files and edit different file. Other features include:

DiffUse - A Tool to Compare Text Files in Linux

  1. Syntax highlighting
  2. Keyboard shortcuts for easy navigation
  3. Supports unlimited undo
  4. Unicode support
  5. Supports Git, CVS, Darcs, Mercurial, RCS, Subversion, SVK and Monotone

7. XXdiff – Diff and Merge Tool

XXdiff is a free, powerful file and directory comparator and merge tool that runs on Unix like operating systems such as Linux, Solaris, HP/UX, IRIX, DEC Tru64. One limitation of XXdiff is its lack of support for unicode files and inline editing of diff files.

It has the following list of features:

xxdiff Tool

  1. Shallow and recursive comparison of two, three file or two directories
  2. Horizontal difference highlighting
  3. Interactive merging of files and saving of resulting output
  4. Supports merge reviews/policing
  5. Supports external diff tools such as GNU diff, SIG diff, Cleareddiff and many more
  6. Extensible using scripts
  7. Fully customizable using resource file plus many other minor features

8. KDiff3 – – Diff and Merge Tool

KDiff3 is yet another cool, cross-platform diff and merge tool made from KDevelop. It works on all Unix-like platforms including Linux and Mac OS X, Windows.

It can compare or merge two to three files or directories and has the following notable features:

KDiff3 Tool for Linux

  1. Indicates differences line by line and character by character
  2. Supports auto-merge
  3. In-built editor to deal with merge-conflicts
  4. Supports Unicode, UTF-8 and many other codecs
  5. Allows printing of differences
  6. Windows explorer integration support
  7. Also supports auto-detection via byte-order-mark “BOM”
  8. Supports manual alignment of lines
  9. Intuitive GUI and many more

9. TkDiff

TkDiff is also a cross-platform, easy-to-use GUI wrapper for the Unix diff tool. It provides a side-by-side view of the differences between two input files. It can run on Linux, Windows and Mac OS X.

Additionally, it has some other exciting features including diff bookmarks, a graphical map of differences for easy and quick navigation plus many more.

Having read this review of some of the best file and directory comparator and merge tools, you probably want to try out some of them. These may not be the only diff tools available you can find on Linux, but they are known to offer some the best features, you may also want to let us know of any other diff tools out there that you have tested and think deserve to be mentioned among the best.

Читайте также:  Шейпер трафика на linux

Источник

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