Compare files and directories linux

Diff files present in two different directories

I have two directories with the same list of files. I need to compare all the files present in both the directories using the diff command. Is there a simple command line option to do it, or do I have to write a shell script to get the file listing and then iterate through them?

9 Answers 9

You can use the diff command for that:

This will output a recursive diff that ignore spaces, with a unified context:

  • b flag means ignoring whitespace
  • u flag means a unified context (3 lines before and after)
  • r flag means recursive

I have also found that —brief option is useful; you get a list of changed files and can process them separately

-urb would also be easy to remember for urban people. Too bad I live in the countryside. I think I’ll use -bru («bru» means «daughter in law», in French).

If you are only interested to see the files that differ, you may use:

diff -qr dir_one dir_two | sort 

Option «q» will only show the files that differ but not the content that differ, and «sort» will arrange the output alphabetically.

It is great! Also works efficiently — i.e. if a whole sub-directory exist only in one of the dirs, its stops recurring into it and reports only the sub-directory itself (without it successor contents). very nice!!

This is a really good first step: see what, if anything, does differ, and then do a detailed diff on each pair of differing files. One’s approach would be wildly different if only one or two files differed versus if dozens of files differed. I could get lost in the output from diff ing dozens of files at once!

Also, can include -x PATTERN in command to exclude certain subdirectories. For example, diff -qr repo1 repo2 -x «.git» will compare two directories (repo1 and repo2) but will exclude files in .git folder of respective directories.

Diff has an option -r which is meant to do just that.

Читайте также:  Работа с флешками linux

diff can not only compare two files, it can, by using the -r option, walk entire directory trees, recursively checking differences between subdirectories and files that occur at comparable points in each tree.

$ man diff . -r --recursive Recursively compare any subdirectories found. . 

Another nice option is the über-diff-tool diffoscope:

It can also emit diffs as JSON, html, markdown, .

If you specifically don’t want to compare contents of files and only check which one are not present in both of the directories, you can compare lists of files, generated by another command.

-printf ‘%P\n’ tells find to not prefix output paths with the root directory.

I’ve also added sort to make sure the order of files will be the same in both calls of find .

The grep at the end removes information about identical input lines.

If it’s GNU diff then you should just be able to point it at the two directories and use the -r option.

for i in $(\ls -d ./dir1/*); do diff $ dir2; done 

N.B. As pointed out by Dennis in the comments section, you don’t actually need to do the command substitution on the ls. I’ve been doing this for so long that I’m pretty much doing this on autopilot and substituting the command I need to get my list of files for comparison.

Also I forgot to add that I do ‘\ls’ to temporarily disable my alias of ls to GNU ls so that I lose the colour formatting info from the listing returned by GNU ls.

Источник

Compare Two Directories in the Linux Command Line

Want to see how the content of the two directories differs? Use the diff command and see what files are identical or different.

How do you compare two files in Linux? You use the diff command.

But how do you compare two folders in Linux? You still use the diff command.

It is easier to visualize the difference between two directories using a GUI tool.

In this tutorial, I’ll share how you can use the diff command to compare directories. I will also discuss a GUI tool called Meld.

The tree command shows the structures of the two directories I use in the examples.

setup for comparing two directories in Linux

So let’s start this tutorial with the CLI method.

Use the diff command to compare directories in Linux

To use the diff command, you will have to follow a simple syntax:

diff -qr Directory-1 Directory-2

To find the differences, you will have to use the -q option which will report only when the difference is found.

Читайте также:  Linux проверить количество ядер

compare two directories in linux

But if you notice carefully, the diff command only looked on file level 1. By default, it won’t look for the files inside the subdirectory.

To perform the search including the subdirectory, you will have to use the -r flag:

compare directories recursively in linux

But what if you want to know the similar files too?

You can easily do that using the -s flag. So if you will use both flags -q and -s , it will show both identical and different files of directories:

find identical and different files of multiple directories in linux

The diff command shows what files differ in the directories. To see the difference, you can run the diff command again on the files to see how their content differs.

Use GUI to compare directories in Linux

If you are not a terminal fan and want to compare the directories in the easiest way possible, use Meld.

Meld is a GUI tool that allows you to check and merge differences.

You’ll have to install it first. In Ubuntu/Debian, use:

It is also available as a flatpak:

flatpak install flathub org.gnome.meld

If you haven’t configured flatpak on your system, check out our detailed guide on how to set up flatpak on various Linux distros.

Once you are done with the installation, open the Meld from your system menu and follow the three easy steps:

select directories to compare on meld

  1. Select the Directory comparison
  2. Choose directories to compare
  3. Click on the Compare button

Once you click on the compare button, it will show you matching and different files available in the selected directories:

compare directories using meld

The ones which are marked with stars are the exact match.

Whereas filenames highlighted with green are only available to that respective directory.

Looking for more tools to compare?

If you are looking for more tools to compare files with various features, we already have a dedicated guide for that:

And if you have any queries related to this guide or want to suggest me what should I cover next, let me know in the comments.

Источник

How to Find Difference Between Two Directories Using Diff and Meld Tools

In an earlier article, we reviewed 9 best file comparison and difference (Diff) tools for Linux and in this article, we will describe how to find the difference between two directories in Linux.

Читайте также:  Add user to group linux usermod

Normally, to compare two files in Linux, we use the diff – a simple and original Unix command-line tool that shows you the difference between two computer files; compares files line by line and it is easy to use, comes with pre-installed on most if not all Linux distributions.

The question is how do we get the difference between two directories in Linux? Here, we want to know what files/subdirectories are common in the two directories, those that are present in one directory but not in the other.

The conventional syntax for running diff is as follows:

$ diff [OPTION]… FILES $ diff options dir1 dir2

By default, its output is ordered alphabetically by file/subdirectory name as shown in the screenshot below. In this command, the -q switch tells diff to report only when files differ.

$ diff -q directory-1/ directory-2/

Difference Between Two Directories

Again diff doesn’t go into the subdirectories, but we can use the -r switch to read the subdirectories as well like this.

$ diff -qr directory-1/ directory-2/

Using Meld Visual Diff and Merge Tool

There is a cool graphical option called meld (a visual diff and merge tool for the GNOME Desktop) for those who enjoy using the mouse, you can install it as follows.

$ sudo apt install meld [Debian/Ubuntu systems] $ sudo yum install meld [RHEL/CentOS systems] $ sudo dnf install meld [Fedora 22+]

Once you have installed it, search for “meld” in the Ubuntu Dash or Linux Mint Menu, in Activities Overview in Fedora or CentOS desktop and launch it.

You will see the Meld interface below, where you can choose file or directory comparison as well as version control view. Click on directory comparison and move to the next interface.

Meld Comparison Tool

Select the directories you want to compare, note that you can add a third directory by checking the option “3-way Comparison”.

Select Comparison Directories

Once you selected the directories, click on “Compare”.

Listing Difference Between Directories

In this article, we described how to find the difference between two directories in Linux. If you know any other commandline or gui way don’t forget to share your thoughts to this article via the comment section below.

Источник

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