Comparing files in Linux. An overview of tools to visually compare and resolve merge conflicts Compare files by size linux

), their comparisons, and the GUI client comparisons for them. There were also discussions about IDE plugins for working with git and mercurial. But practically there was no information about visual comparison and merge conflict resolution tools.

I recently "jumped" from mercurial (which I still find more convenient and logical) to git, because the vast majority of projects that I'm interested in use git and are hosted on github. In this regard, the question arose of revising the arsenal of tools, in particular the question tool selection visual comparison and merging (diff and merge). In order to make up for the lack of information on Habré, I decided to write this mini-review. As they say - in hot pursuit.

Under the cat you will also find examples of Git settings for use with DiffMerge and WinMerge under Windows. I think it will save a lot of time.

Name Peculiarities Platform

KDiff3

Git and WinMerge

1) Add to the directory c: / Git / libexec / git-core / mergetools /
a winmerge file with the following content:

Diff_cmd () ("c: / Program Files (x86) /WinMerge/WinMergeU.exe" \ "$ LOCAL" "$ REMOTE"> / dev / null 2> & 1) merge_cmd () ("c: / Program Files (x86 ) /WinMerge/WinMergeU.exe "\" $ PWD / $ LOCAL "" $ PWD / $ REMOTE "" $ PWD / $ MERGED "> / dev / null 2> & 1 status = $?)
When Git cannot automatically merge changes, a merge conflict occurs and merge markers (<<<<<<<, =======, и >>>>>>>). They are required to resolve the conflict using third-party tools.
Consider the file readme.txt which is the result of doing a branch merge master and new in the above example:

<<<<<<< HEAD master str ======= new str >>>>>>> new
We can open the conflict file using WinMerge to resolve the conflict.

This will open the two-way merge tool:

Based on the described logic, we will rewrite the merge command merge_cmd in the following way:
merge_cmd () ("c: / Program Files (x86) /WinMerge/WinMergeU.exe" \ "$ MERGED"> / dev / null 2> & 1 status = $?)
In fact, both of the above options are equivalent.

2) Let's edit .gitconfig
tool = winmerge cmd = "winmerge" tool = winmerge cmd = "winmerge" trustExitCode = false keepBackup = false
the last line cancels saving backup files in the repository directory.

3) Let's create a conflict when merging two branches (see example using DiffMerge).
git difftool master new // compare the two branches

To resolve the conflict when merging branches, use the command
git mergetool

), their comparisons, and the GUI client comparisons for them. There were also discussions about IDE plugins for working with git and mercurial. But practically there was no information about visual comparison and merge conflict resolution tools.

I recently "jumped" from mercurial (which I still find more convenient and logical) to git, because the vast majority of projects that I'm interested in use git and are hosted on github. In this regard, the question arose of revising the arsenal of tools, in particular the question tool selection visual comparison and merging (diff and merge). In order to make up for the lack of information on Habré, I decided to write this mini-review. As they say - in hot pursuit.

Under the cat you will also find examples of Git settings for use with DiffMerge and WinMerge under Windows. I think it will save a lot of time.

Name Peculiarities Platform

KDiff3

Git and WinMerge

1) Add to the directory c: / Git / libexec / git-core / mergetools /
a winmerge file with the following content:

Diff_cmd () ("c: / Program Files (x86) /WinMerge/WinMergeU.exe" \ "$ LOCAL" "$ REMOTE"> / dev / null 2> & 1) merge_cmd () ("c: / Program Files (x86 ) /WinMerge/WinMergeU.exe "\" $ PWD / $ LOCAL "" $ PWD / $ REMOTE "" $ PWD / $ MERGED "> / dev / null 2> & 1 status = $?)
When Git cannot automatically merge changes, a merge conflict occurs and merge markers (<<<<<<<, =======, и >>>>>>>). They are required to resolve the conflict using third-party tools.
Consider the file readme.txt which is the result of doing a branch merge master and new in the above example:

<<<<<<< HEAD master str ======= new str >>>>>>> new
We can open the conflict file using WinMerge to resolve the conflict.

This will open the two-way merge tool:

Based on the described logic, we will rewrite the merge command merge_cmd in the following way:
merge_cmd () ("c: / Program Files (x86) /WinMerge/WinMergeU.exe" \ "$ MERGED"> / dev / null 2> & 1 status = $?)
In fact, both of the above options are equivalent.

2) Let's edit .gitconfig
tool = winmerge cmd = "winmerge" tool = winmerge cmd = "winmerge" trustExitCode = false keepBackup = false
the last line cancels saving backup files in the repository directory.

3) Let's create a conflict when merging two branches (see example using DiffMerge).
git difftool master new // compare the two branches

To resolve the conflict when merging branches, use the command
git mergetool

Diff usually generates output in the following format:

$ diff file1 file2 2c2< Helllo --- >Hello

But there is a special command line parameter (-y) that tells the utility to display the data in two separate columns. And this is an example of such an output:

$ diff -y file1 file2 Hi Hi Helllo | Hello bye bye

Obviously, when using this output format, the "|" used to mark differing lines.

5. Hiding identical lines

If you look closely at the output from the previous example (from section 4 above), you will find that when you use the -y command line option, diff will output not only the modified but also the identical lines of the files being processed. In the event that you need to remove identical lines from the output, you can use the --suppress-common-lines option.

$ diff -y --suppress-common-lines file1 file2 Helllo | Hello

6. Displaying names of C functions containing modified code

When using diff to compare two C source files, a special option (-p) can be used to tell the utility to display the names of functions in which changes were found. For example, suppose you want to compare the following two C source files:

File named file1.c:

#include void compare (float x, float y) (if (x == y) // incorrect way of comparison (printf ("\ n EQUAL \ n");)) int main (void) (compare (1.234, 1.56789); return 0;)

File named file2:

#include void compare (float x, float y) (if (x == y) (printf ("\ n EQUAL \ n");)) int main (void) (compare (1.234, 1.56789); return 0;)

This is the result of a normal comparison of these files:

$ diff file1.c file2.c 5c5< if(x == y) // некорректный способ сравнения --- >if (x == y)

And this is the result of comparing the same files using the -p parameter:

$ diff -p file1.c file2.c *** file1.c 2016-12-29 11: 45: 36.587010816 +0530 --- file2.c 2016-12-29 11: 46: 39.823013274 +0530 **** *********** *** 2.8 **** void compare (float x, float y) (! If (x == y) // incorrect way of comparison (printf ("\ n EQUAL \ n ");) --- 2,8 ---- void compare (float x, float y) (! If (x == y) (printf (" \ n EQUAL \ n ");)

Obviously, if you use the -p command line option, diff generates more verbose output, marking the changed lines with an exclamation point ("!").

7. Recursive comparison of the contents of subdirectories

The diff utility also allows recursive comparison of the contents of subdirectories, but this mode of operation is not enabled by default. I meant that when using this command:

$ diff diff-files / second-diff-files / diff diff-files / file1 second-diff-files / file1 1c1< Hi --- >i diff diff-files / file2 second-diff-files / file2 2c2< Hello --- >ello

The diff utility will only compare files from top-level directories, but if the -r parameter is used (which activates the recursive file comparison mode), even those files that are in subdirectories will be compared:

$ diff -r diff-files / second-diff-files / diff -r diff-files / file1 second-diff-files / file1 1c1< Hi --- >i diff -r diff-files / file2 second-diff-files / file2 2c2< Hello --- >ello diff -r diff-files / more-diff-files / file1 second-diff-files / more-diff-files / file1 1c1< Hi --- >i diff -r diff-files / more-diff-files / file2 second-diff-files / more-diff-files / file2 2c2< Hello --- >ello

8. Treat missing files as empty

The diff utility also provides an option that you can use to tell it to treat missing files as empty. If you compare the files named file1 and file3 (and the last file does not exist), by default diff will display an error message:

$ diff file1 file3 diff: file3: No such file or directory

There is nothing wrong; in fact, this behavior is quite reasonable. However, there are times when you want to avoid displaying error messages (perhaps when using diff within shell scripts) where you can use the -N option to treat missing files as empty and continue comparing files.

$ diff -N file1 file3 1,5d0< Hi < < Helllo < < Bye

Conclusion

If you carefully read both articles in the series and independently repeated all the command examples discussed in them, then it is quite possible to say that you have mastered all the basic skills of working with the utility in question. Of course, we would not be able to discuss all diff-related issues within the framework of several articles, but nevertheless, all the main features and functions of this utility were somehow mentioned in them.

If you want to know a little more about this utility, you can always refer to its man page. Needless to say, you should periodically use the utility with different sets of parameters to simulate different operating situations.



Comparing two files in linux terminal (6)

Here is my solution for this:

Mkdir temp mkdir results cp / usr / share / dict / american-english ~ / temp / american-english-dictionary cp / usr / share / dict / british-english ~ / temp / british-english-dictionary cat ~ / temp / american -english-dictionary | wc -l> ~ / results / count-american-english-dictionary cat ~ / temp / british-english-dictionary | wc -l> ~ / results / count-british-english-dictionary grep -Fxf ~ / temp / american-english-dictionary ~ / temp / british-english-dictionary> ~ / results / common-english grep -Fxvf ~ / results / common-english ~ / temp / american-english-dictionary> ~ / results / unique-american-english grep -Fxvf ~ / results / common-english ~ / temp / british-english-dictionary> ~ / results / unique-british -english

There are two files named "A.txt" and "B.txt", y which is a list of words. Now I want to check what words are added to "A.txt" and are not in "B.txt" .

I need an efficient algorithm as I need to compare two dictionaries.

You can use the linux diff tool to compare two files. To filter the required data, you can use --changed-group-format options and --unchanged-group-format .

The following three options can be used to select the appropriate group for each option:

    "% <" получить строки из FILE1

    "%>" gets lines from FILE2

    "" (empty line) to remove lines from both files.

For instance: diff --changed-group-format = "%<" --unchanged-group-format = "" file1.txt file2.txt

[tmp] # cat file1.txt test one test two test three test four test eight [tmp] # cat file2.txt test one test three test nine [tmp] # diff --changed-group-format = "%<" --unchanged-group-format="" file1.txt file2.txt test two test four test eight

If you prefer the diff output style from git diff, you can use it with the --no-index flag to compare files outside the git repository:

Git diff --no-index a.txt b.txt

Using a couple of files with 200K file name strings each, I compared (with the built-in time command) this approach and some of the other answers here:

Git diff --no-index a.txt b.txt # ~ 1.2s comm -23<(sort a.txt) <(sort b.txt) # ~0.2s diff a.txt b.txt # ~2.6s sdiff a.txt b.txt # ~2.7s vimdiff a.txt b.txt # ~3.2s

comm seems to be the fastest by far, whereas git diff --no-index seems to be the fastest approach for diff-style output.

Update 2018-03-25 In fact you can omit the --no-index flag if you are not in a git repository and want to compare the unreproducible files in that repository. From the man pages:

This form is for comparing the data of two paths in the file system. You can omit the -no-index parameter when running a command in a Git-controlled working tree and at least one of the waypoints outside of the working tree, or when running a command outside of a Git-controlled working tree.

Use comm -13 (sorted files required) :

$ cat file1 one two three $ cat file2 one two three four $ comm -13<(sort file1) <(sort file2) four

Sort them and use comm:

Comm -23<(sort a.txt) <(sort b.txt)

comm compares (sorts) the input files and prints out three columns by default: strings that are unique to a, strings that are unique to b, and strings that are present in both. By specifying -1, -2 and / or -3 you can suppress the corresponding output. Therefore comm -23 ab only lists records that are unique to a. I am using the syntax<(...) для сортировки файлов на лету, если они уже отсортированы, вам это не нужно.

if you have vim installed try this:

Vimdiff file1 file2

Vim -d file1 file2

you will find it fantastic.

It is often necessary for webmasters or site owners to compare two files by content. This article will show you how to compare two files with each other. It describes all the methods I know of for comparing text files and scripts (html, css, php, and so on).

Method 1. Meld

Meld- a graphical tool for getting differences and merging two files, two directories. Meld is a visual file and directory comparison and merging tool for Linux. Meld is aimed primarily at developers. However, it can be useful to any user who needs a good tool for comparing files and directories.

In Meld, you can compare two or three files, or two or three directories. You can view a working copy from popular version control systems such as CVS, Subversion, Bazaar-NG, and Mercurial. Meld is available for most linux distributions (Ubuntu, Suse, Fedora, etc.), and is present in their main repositories.

# aptitude install meld

Method 2. Comparing the contents of two files in the WinMerge program.

The free program WinMerge allows you to compare not only the contents of files, it also compares the contents of entire folders. WinMerge is an Open Source Compare and Merge tool for Windows. WinMerge can compare both files and folders, displaying differences in visual textual form that are easy to understand and handle.

After installation, open the menu item "File" - "Open". Select files to compare. To do this, click on the "Browse" button and select the file. After selecting the files, click on the "OK" button.

You can also edit files in WinMerge. After closing the comparison window, the program will offer to save the changes in the files.

Method 3.diff

diff is a file comparison utility that displays the difference between two files.

    To compare directories use this command: $ diff -qr< current-directory> < backup-directory>

Method 4. Kompare

Kompare - Displays differences between files. Knows how to compare the contents of files or directories, as well as create, display and apply patch files. Kompare is a graphical diff utility that allows you to find differences in files as well as combine them. It is written in Qt and designed primarily for KDE. Here are its main features:

    Support for multiple diff formats;

    Support linux file and directory comparison;

    Support for viewing diff files;

    Customizable interface;

    Create and apply patches to files.

Method 5. Comparing files in Total Commander

    Supported operating systems: Windows

Total Commander has a tool for comparing files by content, where you can not only compare content, but also edit and copy it from one file to another.

After starting Total Commander - in one of the panels select (Insert key) the first file for comparison - in the second panel open the folder with the second file and put the cursor on it. We call the program for comparison: "Files → Compare by content".

To make changes to the file, just click on the "Edit" button. The program includes the functions of copying and rolling back, searching and changing the encoding. If you have made changes to the file, then after closing the comparison window, you will be prompted to save the changes.

Method 6. Comparing files in Notepad ++

    Supported operating systems: Windows, can run on Linux

Notepad ++ cannot compare files. For this functionality to appear in Notepad ++, you need to install the "Compare" plugin.

Start the editor - go to the menu item "Plugins" - "Plugin Manager" - "Show Plugin Manager". In the new window, select the plugin "Compare" and press the button "Install".

After installing the plugin, open two files and select the menu "Plugins" - "Compare" - "Compare (Alt + D)". The file comparison result will be presented in separate panels. There will be a warning sign next to the lines in which the differences were found.

Method 7. Comparing files using the Windows command line

Comparing using the Windows command line (cmd.exe) does not allow you to edit files, but you can simply compare the contents of the files using this method.

To invoke the Windows Command Prompt, go to Start - All Programs - Accessories - Command Prompt, or press Windows Key + R, type cmd and press Enter.

At the command prompt, enter the command:

fc / N path to first file path to second file