Linux compare files in two directories

Compare files in two different directories in Linux

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

diff -ur dir1 dir2 > my.patch cd dir1 patch -p1 < ../my.patch 

#!/bin/bash diff -ur /tmp/testr/adir1 /tmp/test/adir2 >my.patch cd /tmp/anwar/adir1 patch -p1

I am more interested in seeing those files where mis-match found ( file size or if there count differences as well)

The output from diff can be changed and formatted. You need to go through man diff and play around with those options.

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:

#!/bin/bash IFS_SAVE="$IFS" IFS=$'\x0a' for f in $(find dir1 -type f -printf "%f\n"); do < f1="dir1/$f" f2="dir2/$f" cmp --quiet "$f1" "$f2" check=$? if [ $check -eq 0 ] ; then echo -e "OK: $f" elif [ $check -eq 1 ] ; then echo -en "Mismatch FOUND in files: " filesize1=$(stat --printf="%s" "$f1" ) filesize2=$(stat --printf="%s" "$f2" ) echo "$f1" size:"$filesize1" "$f2" size:"$filesize2" check:"$check" #you may put diff . or anything else here else echo "cannot compare files, probably $f2 is missing" fi >; done IFS="$IFS_SAVE" 

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.

Источник

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.

Читайте также:  How to run linux command python

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.

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 compare differences between directories (linux)

I have two directories - one from earlier backup and second from newest backup. How do i compare what changes were made to files in directory from newest backup on Linux? Also how do i display changes in for example text and php files - i'm thinking about something like revision history on wikipedia where you see old version on one side of the screen and newest version on other and changes are highlighted. How do i achieve something like that? edit: How do i also compare remote dir with local?

Читайте также:  Search all processes linux

7 Answers 7

If both from-file and to-file are directories, diff compares corresponding files in both directories, in alphabetical order; this comparison is not recursive unless the -r or --recursive option is given. diff never compares the actual contents of a directory as if it were a file. The file that is fully specified may not be standard input, because standard input is nameless and the notion of ‘‘file with the same name’’ does not apply.

So to compare directories: diff --brief -r dir1 dir2

To compare files side by side: diff --side-by-side file1 file2

You would have to mount the remote directory to the local machine. However, you can remotely compare files like so: ssh REMOTE_SERVER "cat /path/to/some/file" | diff --side-by-side /path-to-some-file - One other thing, the 'sdiff' command works like 'diff --side-by-side' if you want to save some typing.

Use NFS to mount the remote directory. On the remote server(server-b), edit /etc/exports and put the following in it: /path/of/directory/to/share server-a.ip.address(ro,no_root_squash) Start NFS on server-b (/etc/init.d/nfs start) Mount to your local server(server-a) by adding the following to /etc/fstab: server-b.ip.add:/path/of/directory/to/share /mnt/server-b nfs rsize=32768,wsize=32768,rw 0 0 Then on server-a, mkdir /mnt/server-b; mount /mnt/server-b Thanks for the upvotes.

You really want to combine the power of rsync to reduce bandwidth consumption with the power of diff to give you flexible, well um diffs.

cp -R $local $bak rsync $server:$remdir/* $local/ rsync $local/ $server:$remdir/* diff -wur $local $bak 

I guess you could tweak this a bit if you were doing it often use rsync instead of cp in the first line - obviously in the last line you have the full power of diff to format it however you like. Probably with y in the OPs case

Downside of this approach is you end up using twice as much local space, but at less than $1/gig who cares?

  • we are on www1 , comparing with remote www2
  • there is configured public ssh key authentication from local www1 to remote www2
  • we compare as the same user on local www1 and remote www2
find /var/www/html/ -name "*" -exec md5sum -b <> \; | grep -v "/var/www/html/exclude_this dir" > local.md5 ssh www2 "find /var/www/html/ -name '*' -exec md5sum -b <> \; | grep -v /var/www/html/exclude_this dir > remote.md5" scp www2:remote.md5 . diff local.md5 remote.md5

this actually works but 1. you don't need to scp 2. directory paths need to be the same 3. will pollute both directories with extra files 4. file order is not guaranteed to be the same serverfault.com/a/1128839/349476

Читайте также:  Adding linux route add

Do diff old_dir new_dir > diff.txt for side by side differences on the same server.

For example : ABC is existing server and XYZ is your remote server and directory name is 123.

Step 1: Rename existing directory 123 on ABC Server as 123_ABC.

Step 2: Create a new directory on the server ABC:

Step 3: Copy all files from directory 123 on XYZ server to 123_XYZ directory on ABC server:

XYZ/123 > scp * userid@ABC: /123_XYZ 

This will copy all files from the directory on your XYZ server to your ABC server/ 123_XYZ directory.

Step:4 : Do the diff between both directories:

Now go to ABC server and perform diff between 123_ABC and 123_XYZ

ABC > diff 123_ABC 123_XYZ > diff.txt 

The command above will save the diff results in to diff.txt in the same path.

You may compare the differences then.

AIDE Advanced Intrusion Detection Environment (AIDE) is a file integrity checker for UNIX operating systems. Its purpose to provide reporting on the integrity of data on supported file systems. By running AIDE multiple times on the target host you can determine what files are changing. By running AIDE multiple times on different hosts you can determine what files and permissions are different. Then use a gui diff tool on the reported "different" files.

Or use a gui diff tool like meld, guiffy, kdiff3, diff, vimdiff, gvimdiff, Emacs, Kompare, Diffuse, Easydiff, TkDiff or xxdiff. Most will do directory diffs in addition to file diffs. You'll need to mount the remote drive using NFS, SMBFS or SSHFS as others have mentioned.

Or you can use two files with the out put of list of files. And then compare those two files. For example:

/path/to/compare/remote$: ls > remote-files /path/to/compare/local$: ls > local-files 

Download one of the files.

-rw-r--r-- 1 1015 1015 26624 2005-06-14 13:10 FILE.TXT

Use diff ( diff -y remote-files local-files > diff-files ) to compare them side by side. Open the diff-files and check it. Each line with > means a different file.

Источник

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