Skip to content

Solved: Diff files present in two different directories

Diff files present in two different directories

I have two directories with the same list of files. I need to compare all the files present in both the directories using the diff command. Is there a simple command line option to do it, or do I have to write a shell script to get the file listing and then iterate through them?
unix shell diff

You can use the diff command for that:

diff -bur folder1/ folder2/

This will output a recursive diff that ignore spaces, with a unified context:
b flag means ignoring whitespace
u flag means a unified context (3 lines before and after)
r flag means recursive
	
In Windows, what do you use to make that diff command ?
	
You can have access to the diff command by downloading the GNU utilities for Win32.
	
I have also found that --brief option is useful; you get a list of changed files and can process them separately
	
For others wondering, -b ignores whitespace changes and -u gives 3 lines of buffer.
	
It's easier to remember this command if you use rub instead of bur i.e. diff -rub folder1/ folder2/ – 	

If you are only interested to see the files that differ, you may use:

diff -qr dir_one dir_two | sort

Option "q" will only show the files that differ but not the content that differ, and "sort" will arrange the output alphabetically.
	
It is great! Also works efficiently - i.e. if a whole sub-directory exist only in one of the dirs, its stops recurring into it and reports only the sub-directory itself (without it successor contents). very nice!!    	 
	
-q option man!! I love you.	

Diff has an option -r which is meant to do just that.

diff -r dir1 dir2

diff can not only compare two files, it can, by using the -r option, walk entire directory trees, recursively checking differences between subdirectories and files that occur at comparable points in each tree.

$ man diff

...

-r  --recursive
        Recursively compare any subdirectories found.

...

If it's GNU diff then you should just be able to point it at the two directories and use the -r option.

Otherwise, try using

for i in $(\ls -d ./dir1/*); do diff ${i} dir2; done

N.B. As pointed out by Dennis in the comments section, you don't actually need to do the command substitution on the ls. I've been doing this for so long that I'm pretty much doing this on autopilot and substituting the command I need to get my list of files for comparison.

Also I forgot to add that I do '\ls' to temporarily disable my alias of ls to GNU ls so that I lose the colour formatting info from the listing returned by GNU ls.

It's not necessary to use ls: for i in ./dir1/*
	
funky shortcut. +1. I'm so used to doing command substitution though it's automatic and I just use which ever command I need, e.g. sometimes I'm using find. Using ls also lets me play with the the dir listing more, e.g. reverse time based instead of default sequence.	

Here is a script to show differences between files in two folders. It works recursively. Change dir1 and dir2.

(search() { for i in $1/*; do [ -f "$i" ] && (diff "$1/${i##*/}" "$2/${i##*/}" || echo "files: $1/${i##*/}   $2/${i##*/}"); [ -d "$i" ] && search "$1/${i##*/}" "$2/${i##*/}"; done }; search "dir1" "dir2" )

Try this:

diff -rq /path/to/folder1 /path/to/folder2      
	
https://stackoverflow.com/questions/2019857/diff-files-present-in-two-different-directories
Edited by peturbg