在 Linux 上使用 Meld 比较文件夹
Meld 比较
![](/data/attachment/album/201704/28/231515nzyxjbufhebuj1kb.jpg)


我们已经从一个新手的角度[了解](/article-8402-1.html)了 Meld （包括 Meld 的安装），我们也提及了一些 Meld 中级用户常用的小技巧。如果你有印象，在新手教程中，我们说过 Meld 可以比较文件和文件夹。已经讨论过怎么比较文件，今天，我们来看看 Meld 怎么比较文件夹。


**需要指出的是，本教程中的所有命令和例子都是在 Ubuntu 14.04 上测试的，使用的 Meld 版本为 3.14.2。**


### 用 Meld 比较文件夹


打开 Meld 工具，然后选择 <ruby> 比较文件夹 <rt>  Directory comparison </rt></ruby> 选项来比较两个文件夹。


![Compare directories using Meld](/data/attachment/album/201704/28/231528qpi82ckquy5s977c.png)


选择你要比较的文件夹：


![select the directories](/data/attachment/album/201704/28/231528o33klzbunoizrfrd.png)


然后单击<ruby> 比较 <rt>  Compare </rt></ruby>按钮，你会看到 Meld 像图中这样分成两栏比较目录，就像文件比较一样。


![Compare directories visually](/data/attachment/album/201704/28/231531ovcpz0c00j550y8v.png)


分栏会树形显示这些文件夹。你可以在上图中看到 —— 区别之处，不论是新建的还是被修改过的文件 —— 都会以不同的颜色高亮显示。


根据 Meld 的官方文档可以知道，在窗口中看到的每个不同的文件或文件夹都会被突出显示。这样就很容易看出这个文件/文件夹与另一个分栏中对应位置的文件/文件夹的区别。


下表是 Meld 网站上列出的在比较文件夹时突出显示的不同字体大小/颜色/背景等代表的含义。




| **状态** | **表现** | **含义** |
| --- | --- | --- |
| 相同 | 正常字体 | 比较的文件夹中所有文件/文件夹相同。 |
| 过滤后相同 | 斜体 | 文件夹中文件不同，但使用文本过滤器的话，文件是相同的。 |
| 修改过 | 蓝色粗体 | 比较的文件夹中这些文件不同。 |
| 新建 | 绿色粗体 | 该文件/文件夹在这个目录中存在，但其它目录中没有。 |
| 缺失 | 置灰文本，删除线 | 该文件/文件夹在这个目录中不存在，在在其它某个目录中存在。 |
| 错误 | 黄色背景的红色粗体 | 比较文件时发生错误，最常见错误原因是文件权限（例如，Meld 无法打开该文件）和文件名编码错误。 |


Meld 默认会列出比较文件夹中的所有内容，即使这些内容没有任何不同。当然，你也可以在工具栏中单击<ruby> 相同 <rt>  Same </rt></ruby>按钮设置 Meld 不显示这些相同的文件/文件夹 —— 单击这个按钮使其不可用。


 [![same button](/data/attachment/album/201704/28/231531ggxzqp3lt9f9v6qq.png)](https://www.howtoforge.com/images/beginners-guide-to-visual-merge-tool-meld-on-linux-part-3/big/meld-same-button.png) 


![Meld compare buttons](/data/attachment/album/201704/28/231532j1zjx6j2x6mu416s.png)


下面是单击 <ruby> 相同 <rt>  Same </rt></ruby> 按钮使其不可用的截图：


![Directory Comparison without same files](/data/attachment/album/201704/28/231535gffzmprrtyzhsrfn.png)


这样你会看到只显示了两个文件夹中不同的文件（新建的和修改过的）。同样，如果你单击 <ruby> 新建 <rt>  New </rt></ruby> 按钮使其不可用，那么 Meld 就只会列出修改过的文件。所以，在比较文件夹时可以通过这些按钮自定义要显示的内容。


你可以使用工具窗口显示区的上下箭头来切换选择是显示新建的文件还是修改过的文件。要打开两个文件进行分栏比较，可以双击文件，或者单击箭头旁边的 <ruby> 比较 <rt>  Compare </rt></ruby>按钮。


![meld compare arrow keys](/data/attachment/album/201704/28/231535o9t9ge5jr525g255.png)


**提示 1**：如果你仔细观察，就会看到 Meld 窗口的左边和右边有一些小条。这些条的目的是提供“简单的用颜色区分的比较结果”。对每个不同的文件/文件夹，条上就有一个小的颜色块。你可以单击每一个这样的小块跳到它对应的文件/文件夹。


**提示 2**：你总可以分栏比较文件，然后以你的方式合并不同的文件，假如你想要合并所有不同的文件/文件夹（就是说你想要一个特定的文件/文件夹与另一个完全相同），那么你可以用 <ruby> 复制到左边 <rt>  Copy Left </rt></ruby>和 <ruby> 复制到右边 <rt>  Copy Right </rt></ruby> 按钮：


![meld copy right part](/data/attachment/album/201704/28/231537a4k1d4bibwc3w340.png)


比如，你可以在左边的分栏中选择一个文件或文件夹，然后单击 <ruby> 复制到右边 <rt>  Copy Right </rt></ruby> 按钮，使右边对应条目完全一样。


现在，在窗口的下拉菜单中找到 <ruby> 过滤 <rt>  Filters </rt></ruby> 按钮，它就在 <ruby> 相同 <rt>  Same </rt></ruby>、<ruby> 新建 <rt>  New </rt></ruby> 和 <ruby> 修改的 <rt>  Modified </rt></ruby> 这三个按钮下面。这里你可以选择或取消文件的类型，告知 Meld 在比较文件夹时是否显示这种类型的文件/文件夹。官方文档解释说菜单中的这个条目表示“执行文件夹比较时该类文件名不会被查看。”


该列表中条目包括备份文件，操作系统元数据，版本控制文件、二进制文件和多媒体文件。


![Meld filters](/data/attachment/album/201704/28/231538x11mw86mu7z3ydff.png)


前面提到的条目也可以通过这样的方式找到：*浏览->文件过滤*。你可以通过 *编辑->首选项->文件过滤* 为这个条目增加新元素（也可以删除已经存在的元素）。


![Meld preferences](/data/attachment/album/201704/28/231542z7isshswzws2sfi2.png)


要新建一个过滤条件，你需要使用一组 shell 符号，下表列出了 Meld 支持的 shell 符号：




| **通配符** | **匹配** |
| --- | --- |
| \* | 任何字符 (例如，零个或多个字符) |
| ? | 一个字符 |
| [abc] | 所列字符中的任何一个 |
| [!abc] | 不在所列字符中的任何一个 |
| {cat,dog} | “cat” 或 “dog” 中的一个 |


最重要的一点是 Meld 的文件名默认大小写敏感。也就是说，Meld 认为 readme 和 ReadMe 与 README 是不一样的文件。


幸运的是，你可以关掉 Meld 的大小写敏感。只需要打开 *浏览* 菜单然后选择 <ruby> 忽略文件名大小写 <rt>  Ignore Filename Case </rt></ruby> 选项。 ![Meld ignore filename case](/data/attachment/album/201704/28/231543f07f226ctyzc09e4.png)


### 结论


你是否觉得使用 Meld 比较文件夹很容易呢 —— 事实上，我认为它相当容易。只有新建一个文件过滤器会花点时间，但是这不意味着你没必要学习创建过滤器。显然，这取决于你的需求。


另外，你甚至可以用 Meld 比较三个文件夹。想要比较三个文件夹时，你可以通过单击 <ruby> 三向比较 <rt>  3-way comparison </rt></ruby> 复选框。今天，我们不介绍怎么比较三个文件夹，但它肯定会出现在后续的教程中。




---


via: <https://www.howtoforge.com/tutorial/how-to-perform-directory-comparison-using-meld/>


作者：[Ansh](https://www.howtoforge.com/tutorial/how-to-perform-directory-comparison-using-meld/) 译者：[vim-kakali](https://github.com/vim-kakali) 校对：[jasminepeng](https://github.com/jasminepeng)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# How to compare directories with Meld on Linux

### On this page

We've [already covered](https://www.howtoforge.com/tutorial/beginners-guide-to-visual-merge-tool-meld-on-linux/) Meld from a beginner's point of view (including the tool's installation part), and we've also covered some tips/tricks that are primarily aimed at intermediate Meld users. If you remember, in the beginner's tutorial, we mentioned that Meld can be used to compare both files as well as directories. Now that we've already covered file comparison, it's time to discuss the tool's directory comparison feature.

## Compare directories using Meld

To compare two directories using Meld, launch the tool and select the *Directory comparison* option.

Then select the directories that you want to compare:

Once that is done, click the *Compare* button, and you'll see that Meld will compare both directories side by side, like the tool does in case of files:

Of course, these being directories, they are displayed as side-by-side trees. And as you can see in the screenshot above, the differences - whether it's a new file or a changed file - are highlighted in different colors.

According to Meld's official documentation, each file or folder that you see in the comparison area of the window has a state of its own. A state basically reveals how a particular file/folder is different from the corresponding entry in the other directory.

The following table - taken from the tool's website - explains in details the folder comparison states in Meld.

|
|
|

Same | Normal font | The file/folder is the same across all compared folders. |

Same when filtered | Italics | These files are different across folders, but once text filters are applied, these files become identical. |

Modified | Blue and bold | These files differ between the folders being compared. |

New | Green and bold | This file/folder exists in this folder, but not in the others. |

Missing | Greyed out text with a line through the middle | This file/folder doesn't exist in this folder, but does in one of the others. |

Error | Bright red with a yellow background and bold | When comparing this file, an error occurred. The most common error causes are file permissions (i.e., Meld was not allowed to open the file) and filename encoding errors. |


By default, Meld shows all the contents of the folders being compared, even if they are same (meaning there's no difference between them). However, you can ask the tool to not display these files/directories by clicking the *Same* button in the toolbar - the click should disable this button.

For example, here's our directory comparison when I clicked and disabled the *Same* button:

So you can see that only the differences between the two directories (new and modified files) are shown now. Similarly, if you disable the *New* button, only the modified files will be displayed. So basically, you can use these buttons to customize what kind of changes are displayed by Meld while comparing two directories.

Coming to the changes, you can hop from one change to another using the up and down arrow keys that sit above the display area in the tool's window, and to open two files for side-by-side comparison, you can either double click the name of any of the files, or click the *Compare* button that sits beside the arrows.

**Note 1**: If you observe closely, there are bars on the left and right-hand sides of the display area in Meld window. These bars basically provide "a simple coloured summary of the comparison results." For each differing file or folder, there's a small colored section in these bars. You can click any such section to directly jump to that place in the comparison area.

**Note 2**: While you can always open files side by side and merge changes the way you want, in case you want all the changes to be merged to the corresponding file/folder (meaning you want to make the corresponding file/folder exactly same) then you can use the *Copy Left* and *Copy Right* buttons:

For example, select a file or folder in the left pane and click the *Copy Right* button to make the corresponding entry in the right pane exactly same.

Moving on, there's a *Filters* drop-down menu that sits just next to the *Same*, *New*, and *Modified* trio of buttons. Here you can select/deselect file types to tell Meld whether or not to show these kind of files/folders in the display area during a directory comparison. The official documentation explains the entries in this menu as "patterns of filenames that will not be looked at when performing a folder comparison."

The entries in the list include backups, OS-specific metadata, version control, binaries, and media.

The aforementioned menu is also accessible by heading to *View->File Filters*. You can add new elements to this menu (as well as remove existing ones if you want) by going to *Edit->Preferences->File Filters*.

To create a new filter, you need to use shell glob patterns. Following is the list of shell glob characters that Meld recognises:

|
|

* | anything (i.e., zero or more characters) |

? | exactly one character |

[abc] | any one of the listed characters |

[!abc] | anything except one of the listed characters |

{cat,dog} | either "cat" or "dog" |


Finally, an important point worth knowing about Meld is that the case of a file's name plays an important part as comparison is case sensitive by default. This means that, for example, the files README, readme and ReadMe would all be treated by the tool as different files.

Thankfully, however, Meld also provides you a way to turn off this feature. All you have to do is to head to the *View* menu and then select the *Ignore Filename Case* option.

## Conclusion

As you'd agree, directory comparison using Meld isn't difficult - in fact I'd say it's pretty easy. The only area that might require time to learn is creating file filters, but that's not to say you should never learn it. Obviously, it all depends on what your requirement is.

Oh, and yes, you can even compare three directories using Meld, a feature that you can access by *clicking the 3-way comparison* box when you choose the directories that you want to compare. We didn't discuss the feature in this article, but definitely will in one of our future articles.