Linux compare directories recursively

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.

Источник

Разница между двумя каталогами в Linux

Я пытаюсь найти файлы, существующие в одном каталоге, но не в другом, я попытался использовать эту команду:

проблема с вышеуказанной командой в том, что она находит оба файла в dir1 а не dir2 а также файлы dir2 а не dir1 ,

Я пытаюсь найти файлы dir1 а не dir2 только.

вот небольшой пример того, как выглядят мои данные

другой вопрос на мой взгляд, как я могу найти файлы в dir1 а не dir2 или dir3 в одной команде?

14 ответов

пояснение:

diff -r dir1 dir2 показывает, какие файлы находятся только в dir1 и только в dir2, а также изменения файлов, присутствующих в обоих каталогах, если таковые имеются.

diff -r dir1 dir2 | grep dir1 показывает, какие файлы находятся только в dir1

awk для печати только имени файла.

Это должно сделать работу:

параметры объяснены (через diff (1) man page):

  • -r — рекурсивно сравните все найденные подкаталоги.
  • -q — вывод только в том случае, если файлы отличаются.

эта команда даст вам файлы, которые находятся в dir1 и не в директория dir2.

о знак, вы можете Google это, как подмена процесса.

хороший способ сделать это сравнение, чтобы использовать find С md5sum , потом diff .

использовать find чтобы перечислить все файлы в каталоге, вычислите хэш md5 для каждого файла и передайте его в файл:

выполните ту же процедуру в другом каталоге:

Читайте также:  Как настраивать файрвол для windows 10

затем сравните результат двух файлов с «diff»:

эта стратегия очень полезна при сравнении двух каталогов находятся не на одной машине, и вам нужно убедиться, что файлы равны в обоих каталогах.

еще один хороший способ сделать эту работу-использовать git

С наилучшими пожеланиями!

Meld (http://meldmerge.org/) делает большую работу по сравнению каталогов и файлов внутри.

vim DirDiff плагин-еще один очень полезный инструмент для сравнения каталогов.

он не только перечисляет, какие файлы отличаются между каталогами, но также позволяет проверять/изменять с помощью vimdiff файлы, которые отличаются.

другой (возможно, более быстрый для больших каталогов) подход:

на sed команда удаляет первый компонент каталога благодаря)

Это немного поздно, но может кому-то помочь. Не уверен, что diff или rsync выплевывают только имена файлов в голом формате, как это. Спасибо plhn за предоставление этого приятного решения, которое я расширил ниже.

Если вы хотите только имена файлов, так что легко просто скопировать файлы, необходимые в чистом формате, вы можете использовать команду find.

предполагается, что dir1 и dir2 находятся в одной родительской папке. СЭД просто удаляет родительскую папку, чтобы вы могли сравнить яблоки с яблоками. Последний sed просто возвращает имя dir1.

Если вы просто хотите файлов:

аналогично для каталогов:

в принятом ответе также будут перечислены файлы, которые существуют в обоих каталогах, но имеют разное содержимое. Чтобы перечислить только файлы, существующие в dir1, вы можете использовать:

  • diff-r dir1 dir2: сравнить
  • grep ‘Only in’: получить строки, содержащие ‘Only in’
  • grep dir1: получить строки, содержащие dir

неудовлетворенный всеми ответами, так как большинство из них работают очень медленно и производят излишне длинный вывод для больших каталогов, я написал свой собственный скрипт Python для сравнения двух папок.

В отличие от многих других решений, он не сравнивает содержимое файлов. Также он не входит в подкаталоги, которые отсутствуют в другом каталоге. Таким образом, вывод довольно лаконичен, и скрипт работает быстро.

или если вы хотите видеть только файлы из первого каталога:

P. S. Если вам нужно сравнить размеры файлов и файлов хэши для потенциальных изменений, я опубликовал обновленный скрипт здесь: https://gist.github.com/amakukha/f489cbde2afd32817f8e866cf4abe779

упрощенный способ сравнения 2 каталогов с помощью команды DIFF

diff имя файла.1 именем.2 > имя_файла.dat > > Enter

открыть именем.dat после завершения запуска

и вы увидите: Только в имени файла.1: имя.Два Только в: directory_name: name_of_file1 Только в: directory_Name: name_of_file2

kdiff3 имеет приятный интерфейс diff для файлов и каталогов.

Он работает под Windows & Linux.

GNU grep может инвертировать поиск с помощью опции -v . Это делает grep отчет о строках, которые не совпадают. С помощью этого вы можете удалить файлы в dir2 из списка файлов в dir1 .

опции -F -x рассказать grep для выполнения строкового поиска по всей строке.

это скрипт bash для печати команд для синхронизации двух каталогов

Источник

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.

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.

Читайте также:  Nvidia gtx 650 driver linux

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.

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.

Читайте также:  Media creation tool для линукс

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 !

Источник

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