Menu
Is free
check in
home  /  Programs / Comparison of files in Linux for web developer. Comparison of text files in Linux comparison HEX files in Linux

Comparison of files in Linux for web developer. Comparison of text files in Linux comparison HEX files in Linux

Sometimes there is a need to compare several files among themselves. This may be needed when analyzing the difference between several versions of the configuration file or simply to compare various files. In Linux there are several utilities for this, both to work through the terminal and in the graphical interface.

In this article, we will look at how the Linux files are compared. We will analyze the most useful ways for both the terminal and in graphical mode. First, consider how to compare the Linux file using the DIFF utility.

  • Comparison of Diff Files

    The Diff Linux utility is a program that works in the console mode. Its syntax is very simple. Call the utility, pass filesAnd also specify options if necessary:

    $ diff file file1 file2

    You can pass more than two files if necessary. Before moving to examples, let's look at the Options Utilities:

    • -Q. - display only the differences between files;
    • -s. - output only the coincident part;
    • -with - output the desired number of lines after the coincidence;
    • -u. - to output only the required number of lines after the differences;
    • -y - display in two columns;
    • -E. - output in the format of the ED script;
    • -N. - output in RCS format;
    • -a. - compare files as text, even if they are not text;
    • -t. - replace tabs on gaps in the output;
    • -L. - divide on pages and add scraping support;
    • -r. - Recursive comparison of folders;
    • -I. - ignore the register;
    • -E. - ignore changes in tabs;
    • -Z. - not to take into account spaces at the end of the line;
    • -b. - not to take into account spaces;
    • -B. - Do not take into account empty lines.

    These were the main options utility options, now let's look at how to compare Linux files. In the output of the utility other than, directly, displaying changes, displays a string in which is indicated in which line and what was done. This uses such characters:

    • a. - added;
    • d. - removed;
    • c. - changed.

    In addition, lines that differ will be denoted by the symbol<, а те, которые совпадают - символом >.

    Here are the contents of our test files:

    Now let's make a comparison of DIFF files:
    $ diff file1 file2

    As a result, we get a line: 2.3c2.4. It means that rows 2 and 3 have been changed. You can use Options to ignore the register:
    $ Diff -i File1 File2

    You can conclude in two columns:
    $ Diff -Y File1 File2

    And using the -u option, you can create a patch that then can be imposed on the same file by another user:
    $ Diff -u File1 File2

    To handle multiple files in the folder convenient to use the -r option:
    $ Diff -R ~ / TMP1 ~ / TMP2

    For convenience, you can redirect the output of the utility immediately to the file:
    $ Diff -U File1 File2\u003e file.patch

    As you can see, everything is very simple. But not very convenient. More pleasant to use graphic tools.

    Comparison of Linux files with GUI

    There are several excellent tools to compare files in Linux in a graphical interface. You can easily figure out how to use them. Let's look at a few of them:

    1. Kompare.

    KOMPARE is a graphic utility to work with DIFF, which allows you to find differences in files, as well as combine them. It is written on Qt and is designed primarily on KDE. Here is its main features:

  • Support for multiple DIFF formats;
  • Support Comparison of Linux file and directories;
  • Support for viewing DIFF files;
  • Customizable interface;
  • Create and apply patches to files.

2. Diffmerge.

DiffMerge is cross-platform programs for comparing and combining files. Allows you to compare two or three files. Supports editing rows on the fly.

Features:

  • Catalog comparison support;
  • Integration with file viewer;
  • Customizable.

3. Meld.

This is a light tool for comparing and combining files. It allows you to compare files, directories, as well as perform versions control systems. The program is created for developers and has features:

  • Comparison of two and three files;
  • Using custom types and words;
  • Automatic merger mode and action with text sides;
  • Support Git, Mercurial, Subversion, Bazar and much more.

4. Diffuse

Diffuse is another popular and fairly simple tool for comparing and merging files. It is written in Python. Two main features are supported - File Comparison and Version Management. You can edit the files directly during viewing. Main functions:

  • Highlighting syntax;
  • Shortcut keys for convenient navigation;
  • Support for an unlimited cancellation;
  • Support Unicode;
  • Support Git, CVS, Darcs, Mercurial, Rcs, Subversion, SVK and Monotone.

5. XXDIFF.

XXDIFF is a free and very powerful tool for comparing and merging files. But the program has several minuses. This is the lack of Unicode support and editing files.

Features:

  • Surface or recursive comparison of one or two files and directories;
  • Illumination of differences;
  • Interactive association;
  • Support for external comparison tools, such as GNU Diff, Sig Diff, ClearedDiff and much more;
  • Extensibility with scripts;
  • Customizability.

6. KDIFF3.

KDIFF3 is another excellent, free tool for comparing files surrounded by the desktop KDE. It is included in the KDEVELOP program and works on all platforms, including Windows and MacOS. You can compare two Linux files for two or three, or even compare directories. Here are the main features:

  • Displaying differences line and prevail;
  • Support for auto show;
  • Conflict processing during merger;
  • Support Unicode;
  • Display differences;
  • Hand alignment support.

), their comparisons, as well as the comparisons of the GUI clients for them. There were also discussions of plugins to IDE to work with Git and Mercurial. But practically there was no information On the tools of the visual comparison and permission of conflict conflicts.

Recently, I "rearranged" with Mercurial (which still consider it more convenient and logical) on Git, because, the vast majority of projects that I am interested, use Git and hosted on GitHub. In this regard, the question arose about the revision of the tools arsenal, in particular the issue tool selection Visual comparison and merger (Diff and Merge). In order to fill the lack of information on Habré, I decided to write this mini-review. As they say - on hot wakes.

Under the cat, you will also find examples of GIT settings for use with Diffmerge and Winmerge under Windows. I think many will save time.

Name Features Platform

Kdiff3.

Git and Winmerge.

1) add to the directory c: / Git / Libexec / Git-Core / MergeTools /
File WinMerge as follows:

Diff_cmd () ("C: / Program Files (x86) /winmerge/winmergeu.exe" \\ "$ Local" "$ Remote"\u003e / dev / null 2\u003e & 1) merge_cmd () ("C: / Program Files (x86 ) /Winmerge/winmergeu.exe "\\" $ PWD / $ LOCAL "" $ PWD / $ REMOTE "" $ PWD / $ Merged "\u003e / dev / null 2\u003e & 1 status \u003d $?)
When the Git cannot automatically combine changes, merge conflict and conflicting file are added to the conflicting file (fusion markers are added (<<<<<<<, =======, и >\u003e\u003e\u003e\u003e\u003e\u003e). They are needed to resolve the conflict using third-party tools.
Consider the file readme.txt which is formed as a result of merging branches master and New. In the example above:

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

After that, the two-way merge means will open:

Based on the described logic, rewrite the merger team merge_cmd. in the following way:
merge_cmd () ("C: / Program Files (x86) /winmerge/winmergeu.exe" \\ "$ Merged"\u003e / Dev / NULL 2\u003e & 1 status \u003d $?)
In essence, both of the above options are equivalent.

2) Edit .gitconfig
Tool \u003d WinMerge CMD \u003d "WinMerge" Tool \u003d Winmerge CMD \u003d "Winmerge" trustexitcode \u003d false keepbackup \u003d false
The last line cancels saving backup files in the repository directory.

3) Create a conflict when merging two branches (see Example using Diffmerge).
Git Difftool Master New // Compare Two Branches

To resolve the conflict when merging branches, we use the team
Git Mergetool.

To compare two or more files in Linux there is a DIFF command. She can compare as separate filesand catalogs. Consider the syntax, the options of the DIFF command and multiple use examples.

Diff Team syntax

The DIFF command has the following syntax:

Diff [Options] Files or Directory

We specify options and submit two or more files or directories that we need to compare.

Options Diff Team

Consider the main options of the DIFF command. I will consider only those options that I myself use most often.

-E.ignore changes associated with adding the tab symbol in the text.
-b.ignore changes associated with the addition of spaces.
-w.ignore changes associated with the addition of spaces and tabs.
-B.ignore new empty strings.
-P (or -Show-C-Function)show the name of the function function C, which found changes.
-y (or -Side-BY-SIDE)display results in two columns.
-r.view directories recursively.
-X Fileexclude from searching files whose names match with templates in the File file.
-d (or -minimal)try to find as little changes as possible (that is, eliminate false responses).

Examples of using Diff Team

Comparison of two text files

For a simple comparison of two text files with MyFile1 and MyFile2 names, you will run the command in the terminal:

Diff MyFile1 MyFile2.

The output of the DIFF command is convenient to redirect to the file with the DIFF extension. Most text editors In Linux, for example, GEDIT, recognize this file and highlight its syntax. To send the result of the comparison to the Changes.diff file you need to use the flow redirection symbol (\u003e):

Diff MyFile1 MyFile2\u003e Changes.diff

Comparison of directory containing text files

Consider an example of comparing two directories (MyDir1 and MyDir2) that contain text files. The main difference here from the example above is that we will add the -R option, meaning the recursive bypass of the files in the directories.

Diff -R MyDir1 MyDir2\u003e Changes.diff

Now suppose that in the directories in which we compare files are a lot of "garbage", which we should not compare. Create the EXCLUDEFILES file and write templates and file names that we should not compare. For example, the contents of EXCLUDEFILES may be:

* .o ChangeLog * * .bak * .exe

Now you will specify the DIFF command so that it uses our EXCLUDEFILES file when comparing directories:

Diff -R -X EXCLUDEFiles MyDir1 MyDir2\u003e Changes.diff

Thus, we compare the files whose names do not fall under the templates in the EXCLUDEFILES file, for example, vasya.exe or ChangeLog12.

Add a few more options that are described above to improve the comparison result:

Diff -RWBD -X EXCLUDEFILES MYDIR1 MYDIR2\u003e CHANGES.DIFF

We compare the files in MyDir1 and MyDir2 directories, ignoring changes associated with the addition of empty strings, spaces, tabs, and use file name templates in EXCLUDEFILES to exclude from comparison unnecessary files.

Conclusion

For more information on how to use the DIFF command in your linux system You can get by running the command:

MAN DIFF.

There are also programs that allow you to compare files using graphic interface. For example, MELD program, which shows where and what has changed in files.



Comparison of two files in the Linux terminal (6)

Here is my decision for this:

MKDIR TEMP MKDIR RESULTS CP / USR / Share / American-English-English ~ / Temp / American-English-Dictionary CP / USR / Share / Dict / British-English ~ / Temp / British-English-Dictionary Cat ~ / Temp / American -english-dictionary | WC -L\u003e ~ / Results / Count-American-English-Dictionary Cat ~ / Temp / British-English-Dictionary | WC -L\u003e ~ / Results / Count-British-English-Dictionary Grep -fxf ~ / Temp / American-English-Dictionary ~ / Temp / British-Dictionary - Dictionary\u003e ~ / Results / Common-English Grep -Fxvf ~ / Results / Common-English ~ / Temp / American-English-Dictionary\u003e ~ / Results / Unique-American-English Grep -Fxvf ~ / Results / Common-English ~ / Temp / British-English-Dictionary\u003e ~ / Results / Unique-British -English

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

I need an effective algorithm, as I need to compare two vocabulary.

You can use the DIFF tool in Linux to compare two files. To filter the required data, you can use options --Changed-Group-Format and --unchanged-group-format .

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

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

    "%\u003e" Gets rows from File2

    "" (Empty string) to delete rows from both files.

For example: diff --Changed-Group-Format \u003d "%<" --unchanged-group-format = "" file1.txt file2.txt

[TMP] # cat file1.txt test one Test Two Test Three Test Four Test Eight [TSP] # cat file2.txt test one test three test nine [tmp] # diff --Changed-Group-format \u003d "%<" --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 not in the Git repository:

Git Diff --no-index A.Txt B.txt

Using a file of files with a file name lines of 200 thousand each, I compared (with the built-in Time command) this approach and some 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 today, while Git Diff --no-Index seems to be the fastest approach for DIFF style.

Update 2018-03-25 actual You can omit the --no-index flag if you are not in the Git repository and want to compare the non-reproducible files in this repository. With Man pages:

This form is designed to compare the data of two paths in the file system. You can omit the -no-index parameter when you start the command in the work tree, controlled by GIT, and at least one of the paths outside the workshide or when the command is started outside the working tree, managed by Git.

Use COMM -13. (sorted files are 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) input files and by default displays three columns: lines that are unique to A, lines that are unique to b, and lines that are present in both. When specifying -1, -2 and / or -3, you can suppress the corresponding output. Therefore, the COMM -23 AB lists only records that are unique to a. I use syntax<(...) для сортировки файлов на лету, если они уже отсортированы, вам это не нужно.

if you have a VIM installed, try the following:

Vimdiff file1 file2.

Vim -D File1 File2

you will find it fantastic.

), their comparisons, as well as the comparisons of the GUI clients for them. There were also discussions of plugins to IDE to work with Git and Mercurial. But practically there was no information On the tools of the visual comparison and permission of conflict conflicts.

Recently, I "rearranged" with Mercurial (which still consider it more convenient and logical) on Git, because, the vast majority of projects that I am interested, use Git and hosted on GitHub. In this regard, the question arose about the revision of the tools arsenal, in particular the issue tool selection Visual comparison and merger (Diff and Merge). In order to fill the lack of information on Habré, I decided to write this mini-review. As they say - on hot wakes.

Under the cat, you will also find examples of GIT settings for use with Diffmerge and Winmerge under Windows. I think many will save time.

Name Features Platform

Kdiff3.

Git and Winmerge.

1) add to the directory c: / Git / Libexec / Git-Core / MergeTools /
File WinMerge as follows:

Diff_cmd () ("C: / Program Files (x86) /winmerge/winmergeu.exe" \\ "$ Local" "$ Remote"\u003e / dev / null 2\u003e & 1) merge_cmd () ("C: / Program Files (x86 ) /Winmerge/winmergeu.exe "\\" $ PWD / $ LOCAL "" $ PWD / $ REMOTE "" $ PWD / $ Merged "\u003e / dev / null 2\u003e & 1 status \u003d $?)
When the Git cannot automatically combine changes, merge conflict and conflicting file are added to the conflicting file (fusion markers are added (<<<<<<<, =======, и >\u003e\u003e\u003e\u003e\u003e\u003e). They are needed to resolve the conflict using third-party tools.
Consider the file readme.txt which is formed as a result of merging branches master and New. In the example above:

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

After that, the two-way merge means will open:

Based on the described logic, rewrite the merger team merge_cmd. in the following way:
merge_cmd () ("C: / Program Files (x86) /winmerge/winmergeu.exe" \\ "$ Merged"\u003e / Dev / NULL 2\u003e & 1 status \u003d $?)
In essence, both of the above options are equivalent.

2) Edit .gitconfig
Tool \u003d WinMerge CMD \u003d "WinMerge" Tool \u003d Winmerge CMD \u003d "Winmerge" trustexitcode \u003d false keepbackup \u003d false
The last line cancels saving backup files in the repository directory.

3) Create a conflict when merging two branches (see Example using Diffmerge).
Git Difftool Master New // Compare Two Branches

To resolve the conflict when merging branches, we use the team
Git Mergetool.