1

This question already has an answer here:

I have two directories with same files but with some difference in their contents. I want to list down those files which are differing. For example

There are two folders Folder1 and Folder2 with files file1,file2,file3,etc. file2 of Folder1 is not similar to file2 of Folder2.

So my command should list file2.

How can I do that ?

marked as duplicate by lesmana, Thomas Dickey, Archemar, GAD3R, Anthon Sep 6 '16 at 9:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

You can compare directories with diff:

$ diff -qr dirA dirB
Only in dirB: file.txt
Files dirA/README and dirB/README differ
0

I am sure that there are specialized tools for that purpose (fdupes comes to my mind) this simple script should do the job:

#!/usr/bin/env sh

if [ "a$1" = a -o "a$2" = a ]
then
    echo Directory name missing.
    echo Exiting
    exit 1
fi

for i in "$1"/*
do
    filename=$(basename "$i")
    diff "${1}/${filename}" "${2}/${filename}" > /dev/null 2>&1
    if [ "$?" -eq 1 ]
    then
    echo "$filename" is different
    fi
done

exit 0

You have to have diff binary in your $PATH (you probably have it). Usage:

$ ./diff.sh  Folder1 Folder2
7 is different
file2 is different
  • (1) If you only want to know whether (and not how) two files are different, cmp -s file1 file2 is more efficient than diff — and you don't even need to redirect its output (the -s stands for "silent").  (2) I know that the question says "I have two directories with same files", but a more robust directory comparison script would verify that, and not assume it.  (3) diff (and also cmp) will exit with a status of 2 if one or both of the files do not exist, or a fatal error occurs.  It's safer to say that the files are identical if $? is 0, and all other values indicate problems. – G-Man Sep 2 '15 at 10:23
  • @G-Man: thank you for your suggestions. I agree with points 1 and 3 but about 2: I do check whether user pass two directory names. Do you mean I should check whether these directories exist before proceeding? That's a good idea. – Arkadiusz Drabczyk Sep 2 '15 at 11:05
  • No, I mean that all the filenames are the same.  See the sample output in Stéphane Chazelas's answer for an example.  If folder1/green exists but folder2/green doesn't, diff folder1/green folder2/green will fail with exit status 2 and your script won't understand; if folder2/blue exists but folder1/blue doesn't, your script won't even be aware that folder2/blue exists. – G-Man Sep 2 '15 at 16:41
0

Solution 1: You can have a look there: https://askubuntu.com/questions/111495/how-to-diff-multiple-files-across-directories, especially Rinzwind's answer which stipulates that you can do directly diff -qr Folder1 Folder2.

The other solutions are only useful if you want to merge the results.

Solution 2: If you can run GUI application, the best solution would be to use meld or kdiff3.

Solution 3: Another solution would be to use a version control software. For example, with git, you can do:

$ cd Folder1
$ git init
$ git add .
$ git commit -m "First commit"
$ rm -rf *
$ cp -r /PATH/TO/Folder2/* ./

Then, you can use git status or git diff to see the differences. For this solution, you may want to do a backup first. Be aware that rm -rf * do not delete dot files.

Solution 4: Finally, you could have a look to this vim plugin (I haven't tried it yet): https://github.com/will133/vim-dirdiff. I am sure such plugin also exist for Emacs.

Not the answer you're looking for? Browse other questions tagged or ask your own question.