Diff for multiple files

Hello,

I am trying to compare the contents of 4 directories using diff, where I want files that are present in dir1 but not in dir2, dir3 or dir4 I know this command can only compare 2 files, but I was hoping to find a way to get around this.

I have this thought on my mind

diff -q dir1 dir2 | grep 'Only in /dir1' | diff -q dir3 | grep 'Only in /dir1' | diff -q dir4 | grep 'Only in /dir1' | awk '{print $4}' > in dir1 only.txt

The command in non-functional, I am just expressing the idea of performing diff many times on the resulting files, can this be done in Linux?

This might sound a little retarded, any better ideas are welcomed, my target is just to find the unique files in a certain directory.

Thanks,
Error404

It might help if you could clarify which files exactly you are searching for. If, for instance, a file exists in dir1 and dir2, but not in dir3 and dir4, should it be included or not? How about a file existing in dir1, dir2, dir3 but not in dir4? Does "dir1" take any precendence or should a file existing in dir1 and dir2 but not in dir3 and dir4 be treated the same as one existing in dir2 and dir4 but not in dir1 and dir3?

In other words, could you clarify the rules by which you want to find files? A possible solution (maybe not even relying on "diff") wil depend on how you answer these questions.

I hope this helps.

bakunin

Hello bakunin, I am actually looking to do all the possible combinations for these 4 directories, starting with the most simple, a file that exists in dir1, but not in any other directory.

Afterwards, I want to start doing combinations, like available in dir1 and dir3 but not in dir2 and dir4. This sounds like a very basic command to me but, I wasn't able to come up with the right formula to do this.

Thanks,

When I thought you always wanted to work on three directories, I started working on the following script:

#!/bin/ksh
IAm="${0##*/}"
if [ $# -ne 3 ]
then    printf "Usage: %s dir1 dir2 dir3\n" "$IAm" >&2
        exit 1
fi
if [ ! -d "$1" ] || [ ! -d "$2" ] || [ ! -d "$3" ]
then    printf "%s: All three operands must be directories\n" "$IAm" >&2
        exit 2
fi
if [ "x${2:0:1}" = x/ ]
then    d2="$2"
else    d2="$PWD/$2"
fi
if [ "x${3:0:1}" = x/ ]
then    d3="$3"
else    d3="$PWD/$3"
fi
if ! cd "$1"
then    printf "%s: Can't change directory to \"%s\"\n" "$IAm" "$1" >&2
        exit 3
fi
for i in *
do      if [ ! -e "$d2/$i" ]
        then    if [ ! -e "$d3/$i" ]
                then    printf "Only in %s: %s\n" "$1" "$i"
                else    printf "Only in %s and %s: %s\n" "$1" "$3" "$i"
                 fi 
        else    if [ ! -e "$d3/$i" ]
                then    printf "Only in %s and %s: %s\n" "$1" "$2" "$i"
                fi
        fi
done

which produces the output:

Only in dir1: 2.txt
Only in dir1 and dir3: 5.txt
Only in dir1: 6.txt

for the directory hierarchy given in the 1st message in this thread when it is saved in a file named tester and invoked as:

./tester dir1 dir2 dir3
    or
./tester dir1 dir3 dir2

Note that it was written using the Korn shell, but it uses an extension not specified by the standards ( ${var:start:length} to get a substring from a variable). This is at least available in recent versions of bash and ksh versions more recent that November 16, 1988.

Your specification of the output you wanted is very vague, so this might not even be close to what you want; but I hope it will give you some idea of how to approach your problem.

If you want us to pursue your new, more general problem with an unlimited number of directories and unspecified combinations of comparisons, you need to give a much more detailed description of what you want to happen, how you want to specify the options and operands for this command, and how you want the output formatted. Try to write a man page describing the utility you want to create.

Hi.

You may be interested in combine:

       combine - combine sets of lines from two files using boolean operations

SYNOPSIS
       combine file1 and file2

       combine file1 not file2

       combine file1 or file2

       combine file1 xor file2
...

This is one utility in a package, moreutils, available in some distributions (Debian is where I obtained i)t. It is also maintained at moreutils

Best wishes ... cheers, drl