Diff files present in two different directories

UnixShellDiff

Unix Problem Overview


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 Solutions


Solution 1 - Unix

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

Solution 2 - Unix

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.

Solution 3 - Unix

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

diff -r dir1 dir2

Solution 4 - Unix

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.

...

Solution 5 - Unix

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.

Solution 6 - Unix

If you specifically don't want to compare contents of files and only check which one are not present in both of the directories, you can compare lists of files, generated by another command.

diff <(find DIR1 -printf '%P\n' | sort) <(find DIR2 -printf '%P\n' | sort) | grep '^[<>]'

-printf '%P\n' tells find to not prefix output paths with the root directory.

I've also added sort to make sure the order of files will be the same in both calls of find.

The grep at the end removes information about identical input lines.

Solution 7 - Unix

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" )

Solution 8 - Unix

In practice the question often arises together with some constraints. In that case following solution template may come in handy.

cd dir1
find . \( -name '*.txt' -o -iname '*.md' \) | xargs -i diff -u '{}' 'dir2/{}'

Solution 9 - Unix

When working with git/svn or multiple git/svn instances on disk this has been one of the most useful things for me over the past 5-10 years, that somebody might find useful:

diff -burN /path/to/directory1 /path/to/directory2 | grep +++

or:

git diff /path/to/directory1 | grep +++

It gives you a snapshot of the different files that were touched without having to "less" or "more" the output. Then you just diff on the individual files.

Solution 10 - Unix

Try this:

diff -rq /path/to/folder1 /path/to/folder2      

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSudarView Question on Stackoverflow
Solution 1 - UnixLaurent EtiembleView Answer on Stackoverflow
Solution 2 - UnixSureshView Answer on Stackoverflow
Solution 3 - UnixCodeRainView Answer on Stackoverflow
Solution 4 - UnixmikuView Answer on Stackoverflow
Solution 5 - UnixRob WellsView Answer on Stackoverflow
Solution 6 - UnixNO_NAMEView Answer on Stackoverflow
Solution 7 - UnixEvgeny KarpovView Answer on Stackoverflow
Solution 8 - UnixPatrick FrombergView Answer on Stackoverflow
Solution 9 - UnixEamonn KennyView Answer on Stackoverflow
Solution 10 - UnixSonali BirariView Answer on Stackoverflow