- Given two directory trees, how can I find out which files differ by content? [closed]
- 10 Answers 10
- How to Find Difference Between Two Directories Using Diff and Meld Tools
- Using Meld Visual Diff and Merge Tool
- Compare Two Directories in the Linux Command Line
- Use the diff command to compare directories in Linux
- Use GUI to compare directories in Linux
- Looking for more tools to compare?
Given two directory trees, how can I find out which files differ by content? [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
This outputs exactly what the differences are between corresponding files. I’m interested in just getting a list of corresponding files whose content differs. I assumed that this would simply be a matter of passing a command line option to diff , but I couldn’t find anything on the man page. Any suggestions?
With respect to one of the directories, how to get only the files/directories which are extra in the other?
10 Answers 10
diff --brief --recursive dir1/ dir2/
Or alternatively, with the short flags -qr :
If you also want to see differences for files that may not exist in either directory:
diff --brief --recursive --new-file dir1/ dir2/ # with long options diff -qrN dir1/ dir2/ # with short flag aliases
Nice. But shorter is diff -qr dir1/ dir2/ and my extended version to diff -qr dir1/ dir2/ | grep ‘ differ’
@skv Not exactly what the original question asked, but updating the answer to accommodate this question as well.
@MikeMaxwell It needs to be —brief . -brief is interpreted as -b -r -i -e -f , in other words as a set of flags not as a single option.
@daboross: wow, I’ve been using Unix/Linux for a l o n g time, and I never realized there was that distinction between ‘—‘ and ‘-‘. (I don’t think ‘—‘ existed when I got started.) Thanks for the explanation!
— options are called «UNIX options» and — options are called «GNU long options» according to man ps . You should make every program accept long options if it uses any options, for this takes little extra work and helps beginners remember how to use the program. source: gnu.org/software/libc/manual/html_node/Getopt-Long-Options.html, also google.com/search?q=gnu+long+options
It is exactly the same as Mark’s 🙂 But his answer bothered me as it uses different types of flags, and it made me look twice. Using Mark’s more verbose flags it would be:
diff --brief --recursive dir1/ dir2/
I apologise for posting when the other answer is perfectly acceptable. Could not stop myself. working on being less pedantic.
..so does it make sense tu put different answers with JUST a different flavour? IMHO no! Does it make sense tu combine both answers to one consistent answer? yes! 😉
Just a question; what does the q stand for? Is it an abbreviation of something? I can’t find any logic behind the q ..
@kramer65 — it is the same as «—brief», but I guess you wonder why q? Perhaps for quick? «-b» is taken by «ignore changes in the amount of white space» according to the man page.
@sobi3ch You are right, I apologise again. To my defence, I do not think I had the ability to edit the other answer at the time.
I like to use git diff —no-index dir1/ dir2/ , because it can show the differences in color (if you have that option set in your git config) and because it shows all of the differences in a long paged output using «less».
Neat. Who would’ve guessed that git can diff arbitrary directories, not just the repo against its files?
If you comparing (like me) 2 dirs as seperate git projects/repos then you need add —no-index more on stackoverflow.com/a/1792477/473390. I’ve updated @alan-porter answer.
I like this one, I also find that if you add —name-status to the command line, it will just show the file name list with «M/A/D» flags for Modified/Added/Deleted status.
It happens so that both directories are actually containing the .git folder, how can I exclude it from the compare?
rsync --dry-run --recursive --delete --links --checksum --verbose /dir1/ /dir2/ > dirdiff_2.txt # or same in short rsync -nrlcv --delete /dir/ > dirdiff_2.txt
diff --brief --recursive --no-dereference --new-file --no-ignore-file-name-case /dir1 /dir2 > dirdiff_1.txt # or same in short diff -qrN --no-dereference --no-ignore-file-name-case /dir > dirdiff_1.txt
They are functionally equivalent, but performance may vary depending on:
- If the directories are on the same drive, rsync is faster.
- If the directories reside on two separate drives, diff is faster.
This is because diff puts an almost equal load on both directories in parallel, maximizing load on the two drives. rsync calculates checksums in large chunks before actually comparing them. That groups the i/o operations in large chunks and leads to a more efficient processing when things take place on a single drive.
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.
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/
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.
Select the directories you want to compare, note that you can add a third directory by checking the option “3-way Comparison”.
Once you selected the directories, click on “Compare”.
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.
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.
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.
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:
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:
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 the Directory comparison
- Choose directories to compare
- 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:
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.