Find Differences Between Folders and Files Recursively

Written by
Date: 2012-02-29 17:17:00 00:00

Let's say you have two folders which are supposed to have the same information each one of them, but don't really know.

How can you check if they really have the same info? You can use diff

First thing, create a scenario to check how diff works. I'll create two folders with subfolders and files in them so you can see diff in action.

/folder1/code /folder1/compiled /folder1/media /folder1/assets

Now Folder2 structure

/folder2/code /folder2/compiled /folder2/media /folder2/logs

Finally I'll add some files in some of the folders.

In /folder1/code there will be a file named code.sh with this content

cd /tmp/
mkdir tmp.bak/
cp *.bak /tmp.bak/

In /folder2/code there will be the same file code.sh with this content

cd /tmp/
mkdir tmp.back/
cp *.bak /tmp.back/

In the /foder1/media/ there will be a file named movie1.mp4 that is not going to be present in /folder2/media/

Now let's run the command:

diff -r folder1/ folder2/

The output should be something like this:

Only in folder1: assets
diff -r folder1/code/code.sh folder2/code/code.sh
2,3c2,4
< mkdir tmp.bak/
< cp *.bak /tmp.bak/
---
> mkdir tmp.back/
> cp *.bak /tmp.back/
> 
Only in folder2: logs
Only in folder1/media: movie1.mp4

As you can see, diff tells you which sub-folders are only in folder1, and which ones are only in folder2, also the difference in the code.sh file that exists on both sides, as well as the file movie1.mp4 that only exists in folder2

Hope it may be useful for someone sometime.

Note: This is also availabe in Mac OSX