Compare 2 directory trees in linux

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:

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.

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.

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.

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.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

How do I compare two source trees in Linux? [closed]

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 6 years ago .

I have two directories containing source files to a project I’ve inherited with little by way of documentation. How do I compare both directories to make see what the differences are?

5 Answers 5

  • The -u option makes the output a little easier to read.
  • The -r option recurses through all subdirectories
  • The -N and -a options are really only necessary if you wanted to create a patch file.

You can try Meld. It is a wonderful visual diff tool 😉

Will show you a unified recursive diff between the files in dirA and dirB

You may use the diff command in the shell. Or install a tool like KDiff3.

The diff command to compare directories kept telling me that I didn’t have differences, when I knew there were differences.

Instead of using diff directly, I used a sorted list of md5sums and then compared those files with diff:

If the beginning part of the path is causing headaches, then change it. Select the Path1 window and type:

This will replace all instances of path1 with path2 in the first file, and now your diff should only show differences.

Источник

Compare files in two different directories in Linux

A shell script which shows differences in multiple files in two different directories and also possibly create an output file including where all mismatches were found.

File dir1/file1 compare only with dir2/file1 (similarly for other files — file2 compare with file2)

If any changes found: status should be «Miss-match FOUND in file file1 for example» and same thing should do for all other files as well and write a all of the results into a one file

Читайте также:  Неверное имя пользователя при входе windows

Thanks in advance

2 Answers 2

Use the diff command. Using the -r flag you can compare folders recursively:

The output will be in a format which the patch command understands. You can save it in a file and apply those changes to dir1 using

If you deal with text files and want to just see the differences, I would customize the diff output, as hek2mgl suggested. But if you want more control, for example to execute some commands after finding different files or you must compare binary files, you may utilize find and cmp .
Below is the sample, which you may customize:

Depending on your situation (if filenames do not contain spaces, there are no missing files, etc.) you may omit some parts — this was just tailored from a larger script.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!
  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Источник

How best to compare huge directory trees

How can I compare directory trees of huge size?

I am thinking a free tool to make a snapshot of the filesystem structure (listing of files and directories, their size & timestamps) would be ideal so I could compare the snapshot to another one made later.

Treecomp would be great for that but with a huge tree (I mean really huge!) it crashes because it tries to keep it in memory (4GB of memory are not enough).

I worked around the problem by splitting the snapshots into pieces and compare these pieces. But that’s tedious, and the problem can for sure be solved better.

Is there another free (best also open source) tool that I can try out? Or is there another way to do this that I am overlooking?

OS can be Linux or Windows.

12 Answers 12

I’ll try to expand a bit on how to do it with Total Commander (I hope I understood what you want to do).

  • install DiskDir packer plugin (I put a direct link to plugin, if you prefer you can go to plugins page and look for DiskDir plugin
  • after the plugin is installed «pack» the directory you want to track changes of with Alt+F5 and select «lst» from the drop down list in Packer part of the dialog box; this will create a «package» that you can enter by pressing enter, like you would enter a directory and it will show complete contents of the directory
  • when comparing results go to the original directory on the left pane and enter desired snapshot on right pane
  • use «Synchronize Dirs» function, located in Commands menu
  • in Synchronize directories window uncheck compare by contents, check Subdirs and Ignore date (or not if changed date is important) and run comparison
  • window will show you files that are equal (in this case not by contents, only by size), files that are different and files missing on left/right side

Since the snapshot is a plain text file and you are not comparing by contents it should be fast but I never used it for a really huge directory.

This is useful if you are not making backups but only wish to make a snapshot of what contents of the directory was at some point. If you do make backups you can use same tool (Synchronize dirs) to also compare by contents.

There is also an extended version of DiskDir plugin, download link is in the first post. This version enables you to have packages (like zip, 7z. ) show as directories in the snapshot. This would of course increase time to make a snapshot.

Источник

Linux Commando

Initially a Linux command-line interface blog, it has evolved to cover increasingly more GUI app topics. Instead of just giving you information like some man page, I illustrate their usage in real-life scenarios.

Search This Blog

Sunday, May 4, 2008

Compare Directories using Diff in Linux

To compare 2 files, we use the diff command. How do we compare 2 directories? Specifically, we want to know what files/subdirectories are common, what are only in 1 directory but not the other.

Unix old-timers may remember the dircmp command. Alas, that command is not available in Linux. In Linux, we use the same diff command to compare directories as well as files.

Without any option, diffing 2 directories will tell you which files only exist in 1 directory and not the other, and which are common files. Files that are common in both directories (e.g., .bashrc in the above listing) are diffed to see if and how the file contents differ.

Читайте также:  Download windows те ddk

If you are NOT interested in file differences, just add the -q (or —brief ) option.

diff orders its output alphabetically by file/subdirectory name. I prefer to group them by whether they are common, and whether they only exist
in the first or second directory. That is why I piped the output of diff through sort in the above command.

Note that by default diff does not reach into the subdirectories to compare the files and subdirectories at that level. To change its behavior to recursively go down subdirectories, add -r .

37 comments:

Any way to do this across an SSH tunnel?

Assuming both files reside in remote machine.

ssh user@123.123.123.123 diff -rq dir1 dir2

August 20, 2008 at 3:52 PM Anonymous said.

I was hoping for something to compare remote to local.

I’ll just mount the remote filesystem using SSH-fuse or whatever.

September 25, 2008 at 5:09 PM Anonymous said.

This is wrong on some systems. For example on Ubuntu using diff (GNU diffutils) 2.8.1, you must use the -r switch to get the full comparison.

March 5, 2009 at 7:37 AM Anonymous said.

Thanks for the article.
I just backed up 30+ gig of data. I wanted to insure I had a good copy.
I created an empty file in the middle of the directory structure of the backup.
I found it as a file on backup, but not original, and three files that were not the same.
Appreciate the guidance!

@phyzome (he won’t need it anymore, but maybe someone else finds it useful):

If you want to compare files between local and remote machines, look into «rsync». As the name sais, it’s purpose is to sync them, but you can use it without actually changing anything.

Especially if you are interested in missing/additional files AND changed files, rsync is cool, because it can compare the file contents without transfering the files over the wire, which makes it very fast.

In a situation, where I have had to compare remote directories on separate sites quickly and where time stamp was no good, I have used find with cksum to good effect.

Directing the output to a text file for the two directories then left me two text files to compare.

February 25, 2010 at 4:39 AM Tim said.

Do you guys have a recommendation what to do in the following case: I have two potentially different directory trees, say A and B, and I want to know which files somewhere in A but not in B and vice verse.

There are programs like «fdupes» that look for duplicate files across directory trees but they also look for duplicates within A and B. This can be rather painful if A and/or B contains a lot of files.

In my situation I have two directory trees with pictures which I want to unify.

March 26, 2010 at 8:39 AM volty said.

files from A will appear only if more than 1 equals is in A

May 14, 2010 at 3:58 PM Anonymous said.

You can use rsync too.

rsync -avn source-dir/ target-dir/

This will list the files that are different (or new) in source-dir compared to target-dir.

If you run it without the -n option, it will copy the missing (or different) files over to the target.

June 25, 2010 at 1:47 PM Anonymous said.

my directory has objects files . but i nedd to diff between files other than object files.. can some help me..

July 1, 2010 at 10:22 PM Anonymous said.

The rsync command I posted above compares files based on last modification date and size, if you want to compare the CONTENT of the files, you can make rsync use checksums (-c):

rsync -avcn source-dir/ target-dir

This will list the files whose content is different in two directories. Again, if you want to replace the files that are differentin target-dir with the files from source-dir, remove the -n option.

Very nice article. It very useful way for comparison. Thanks a lot.. 🙂

This is a very useful post and will give people peace of mind when backing up files.

I’m using the diff -r command before deleting the original copy of some backups that I’ve transferred to a new RAID array.

Keep in mind I did run rsync twice to do this, but this data is important to me so I want to be sure nothing is missing and that the file integrity is in tact.

if you want the list of colliding filenames:

diff -sq dir1 dir2 | grep -v «Only in»

May 22, 2011 at 4:53 PM Anonymous said.

White text on black background sucks. But thanks for the post.

October 12, 2011 at 5:56 PM Anonymous said.

If you’re looking to compare the contents of two directories, and want to see how they are the same (not how they are different), and getting cmpdir isn’t an option, try this:

diff -q -y -s | grep «are identical$»

It’s ugly and IO intensive, but works for me. The diff is running with the «-s» option to also output items that are identical. The grep then only picks out the stuff that is identical and doesn’t display differences.

Читайте также:  Ошибка установщика windows не удалось выполнить библиотеку dll

November 2, 2011 at 6:55 PM Anonymous said.

Thank you, it helped a lot

December 19, 2011 at 3:44 PM Anonymous said.

I use vimdiff to diff a local file to a remote. i know this is old discussion but eh.

July 19, 2012 at 10:06 AM Oliver said.

Your simple and clear explanation was a great help, just what I needed while I was having nightmares in front of the threatening black shell.

I sincerely thank you 🙂

PS : maybe you could update it with an easy way to export to a text file the results of a diff, but that’s a detail.

That’s certainly an easy one, just redirect the stdout to a file with «>»:

diff -rq dir1 dir2 > dir1-dir2-diffs

I just ran across this today. I was trying to compare directories and got a «command not found» response. I appreciate the information.

I just wish they had just created a command alias with documentation that the command had been deprecated. Imagine the script errors that happen when people pull out an old script or try to build from old sources.

November 26, 2012 at 8:52 AM Anonymous said.

what to do if i want to copy the files which are different to a third directory??

December 24, 2012 at 7:35 AM chk said.

Thank you! Very useful!!

A useful trick I use a lot is:

diff -qr dir1 dir2 | grep -v .svn | sort

That excludes all my .svn directories, which are really irrelevant if you’re doing this..

Thanks man. This post helped me with comparing two directories with lots files.

Great productive tip. Saved me time comparing two directories.

hi. Im student from Informatics engineering nice article,
thanks for sharing 🙂

August 28, 2013 at 12:32 AM BASTA! said.

Is there a way to get a machine-readable rather than human-readable output, particularly for «Files X and Y differ» lines? My problem with this format is that ‘X’ and ‘Y’ are not delimited in any way, so if ‘X’ happens to be ‘foo and bar’ then there is no way to parse these lines unambiguously.

If you want to just compare what files and subdirectories are different in one directory from another, ignoring differences within files and common subdirectories/files, you could add the following to your .bashrc or .bash_profile:

November 11, 2013 at 1:13 PM Anonymous said.

Lots of interesting tips here, but I am still confused. I tried the following commands under Ubuntu 12.04 to compare a directory (recursively) to a copy on a mounted SMB share:

in increasing order of execution time.
The rsync commands produced identical lists containing several directories and files. The diff command, however, did not report any differences. How is this possible?

December 20, 2013 at 4:05 PM Micheal Johnson said.

I normally use ls | sort (or ls -l if I want to include file permissions, ownership, sizes and modification/access times in my comparison, or ls -R if I want to do a recursive comparison). So I use it like this:

ls first_directory | sort >

/tempfile1
ls second_directory | sort >

Of course you have to pipe it through sort as well because otherwise the lines sometimes come out in a different order even if they are the same. I have never had any problems with using this method but it is nice to have a slightly more elegant way of doing this!

September 21, 2014 at 2:17 AM Anonymous said.

It’s a really useful nice post. For those who are interested I added two tools that are really
powerful.

Tools like «rsync» and «diff» are really great. I would also mention «unison» and the new shining «syncany».

UNISON: To sync to folders with unison simply do a:
unison folderA folderB

In contrast to rsync unison does a sync in both directions. Further more unison includes rsync and works on windows, linux and mac. Unison is a very mature and stable software.

SYNCANY:Use
sy status
to show differences
sy up
to upoload differences
sy down
to download differences

syncany has also version control capilities. Nevertheless syncany is in Alpha status as in 2016.

Both tools are for advanced usage and a simple «diff -rq foderA folderB» does the job. With rsync, unison and syncany you can automate tasks, share folders like dropbox and sync them over the internet.

Greetings and hopefully it helped you.

Diff folders by checksums, between ‘local’ and SSH ‘remote’:

rsync —dry-run -aci —delete /local/path/ -e «ssh -i

Do a simulation (-n, —dry-run) only, as if you were archiving (-a, —archive),
comparison based checksum (-c. —checksum), output a change-summary for all updates
( -i, —itemize-changes), as if you were mirroring to destination (—delete).

we can use diff -rq path1(dir)/ path2(dir) > list.txt
all the compared list will be saved in list.txt

February 25, 2017 at 8:52 PM Anonymous said.

Thanks, it works.

May 27, 2019 at 6:26 AM Anonymous said.

Thank you very much, you saved me a lot of time !

Источник

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