Diff for multiple files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Diff for multiple files
# 1  
Old 05-28-2013
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

Code:
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
# 2  
Old 05-28-2013
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
# 3  
Old 05-28-2013
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,
# 4  
Old 05-28-2013
When I thought you always wanted to work on three directories, I started working on the following script:
Code:
#!/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:
Code:
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:
Code:
./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.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 05-28-2013
Hi.
Quote:
Originally Posted by Error404
...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.
You may be interested in combine:
Code:
       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
This User Gave Thanks to drl For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Diff 3 files, but diff only their 2nd column

Guys i have 3 files, but i want to compare and diff only the 2nd column path=`/home/whois/doms` for i in `cat domain.tx` do whois $i| sed -n '/Registry Registrant ID:/,/Registrant Email:/p' > $path/$i.registrant whois $i| sed -n '/Registry Admin ID:/,/Admin Email:/p' > $path/$i.admin... (10 Replies)
Discussion started by: kenshinhimura
10 Replies

2. Shell Programming and Scripting

Help with create multiple directories under diff file systems

Hi, Need help ...I want to create multiple directories in different /file systems using for loop..eg.../ORCL_data01/oradata/orcl/ctl. ../ORCL_data01/oradata/orcl/data. ../ORCL_data01/oradata/orcl/redo. Script :- ========= for dir in `ls -d... (8 Replies)
Discussion started by: Linux6.5
8 Replies

3. UNIX for Dummies Questions & Answers

Using AWK: Extract data from multiple files and output to multiple new files

Hi, I'd like to process multiple files. For example: file1.txt file2.txt file3.txt Each file contains several lines of data. I want to extract a piece of data and output it to a new file. file1.txt ----> newfile1.txt file2.txt ----> newfile2.txt file3.txt ----> newfile3.txt Here is... (3 Replies)
Discussion started by: Liverpaul09
3 Replies

4. Shell Programming and Scripting

diff bw two files

Hi All, I have two files which look as below File1 serial="1" name="abc" type="employee" field="IT" serial="2" name="cde" type="intern" field="Marketing" serial="3" name="pqr" type="contractor" field="IT" serial="4" name="xyz" type="employee" field="Sales" File2 serial="1"... (3 Replies)
Discussion started by: grajp002
3 Replies

5. Shell Programming and Scripting

diff of files

Hi, I have 2 files.I want to check if file1 is contained in file2. A.txt: ----- AAA BBB B.txt: ------ CCC AAA BBB DDD I want to check if A.txt is contained in B.txt. Can it be done using SED ? (12 Replies)
Discussion started by: giri_luck
12 Replies

6. Shell Programming and Scripting

Call single function multiple times diff set of parameters

Okay, not sure if it can be done, I would think it could be done and I'm just having a hard time with it. fun_close_wait(){ ipVar="${1} ${2}" portVar=`cat "${5}" | cut -d' ' -f 1` for ip in $ipVar do for port in $portVar do netstatVar=`netstat -n | grep... (4 Replies)
Discussion started by: cbo0485
4 Replies

7. Shell Programming and Scripting

Diff b/w 2 files

Hi Masters, I have two files named file1 and file2. Both the files contains the same contents with some difference in comments,space.But no content change. I tried to find the diff between the two files to make sure that contents are same. For that i tried diff -ibw file1 file2 But... (1 Reply)
Discussion started by: ecearund
1 Replies

8. Shell Programming and Scripting

Find duplicates from multuple files with 2 diff types of files

I need to compare 2 diff type of files and find out the duplicate after comparing each types of files: Type 1 file name is like: file1.abc (the extension abc could any 3 characters but I can narrow it down or hardcode for 10/15 combinations). The other file is file1.bcd01abc (the extension... (2 Replies)
Discussion started by: ricky007
2 Replies

9. AIX

Locking a file when using VI to prevent multiple-edit sessions by diff users

At the office, we often have to edit one file with VI. We are 4-6 workers doing it and sometimes can be done at the same time. We have found a problem and want to prevent it with a file lock. Is it possible and how ? problem : Worker-a starts edit VI session on File-A at 1PM Worker-b... (14 Replies)
Discussion started by: Browser_ice
14 Replies

10. Shell Programming and Scripting

diff 2 files; output diff's to 3rd file

Hello, I want to compare two files. All records in file 2 that are not in file 1 should be output to file 3. For example: file 1 123 1234 123456 file 2 123 2345 23456 file 3 should have 2345 23456 I have looked at diff, bdiff, cmp, comm, diff3 without any luck! (2 Replies)
Discussion started by: blt123
2 Replies
Login or Register to Ask a Question