Operating System - Linux
1827859 Members
1900 Online
109969 Solutions
New Discussion

How to Compare Files one by one using Shell Script

 
SOLVED
Go to solution
Padma Asrani
Honored Contributor

How to Compare Files one by one using Shell Script

Hello All

I have two directories and I wanted to compare files one by one in these directories using shell script. Can anybody help how should I start looping through files in each directory and compare these line by line and get the difference?

Thanks
Padma
18 REPLIES 18
OldSchool
Honored Contributor

Re: How to Compare Files one by one using Shell Script

2 directories, compare files.....

you're going to have to be a little more specific...

Compare file names only?
Compare like named files?
Text only, or are there binaries, such as executables?
Desired output?

"man diff".....
Padma Asrani
Honored Contributor

Re: How to Compare Files one by one using Shell Script

Hello

These are the text files and I mentioned that I wanted to compare these files line by line.

Regards
Padma
James R. Ferguson
Acclaimed Contributor

Re: How to Compare Files one by one using Shell Script

Hi Padma:

Have a look at the manpages for 'diff' and for 'dircmp':

http://docs.hp.com/en/B3921-60631/diff.1.html

http://docs.hp.com/en/B3921-60631/dircmp.1.html

Regards!

...JRF...
TwoProc
Honored Contributor

Re: How to Compare Files one by one using Shell Script

Since you've not given us much to go on, I'll have to just make assumptions.

Assumptions:
1) both directories have all the same number of files, only their content may be different.
2) both directories are siblings of the same parent directory.
3) There are no subdirectories that you want to scan.
4) There are no missing files that you are, or are not trying to compare.
5) The two sibling directories are named "a" and "b". And they are children of the current directory "."

$> cd a
for i in *
do
diff $i ../b/$i
done
We are the people our parents warned us about --Jimmy Buffett
Peter Nikitka
Honored Contributor

Re: How to Compare Files one by one using Shell Script

Hi,

a call of
dircmp -d -s dira dirb

will
- no identical file
- flag files only present in one of the directories
- list a diff-report of identical filenames in both directories

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
TwoProc
Honored Contributor

Re: How to Compare Files one by one using Shell Script

Wow,

I forgot all about dircmp.

> Do what they say, use dircmp.
We are the people our parents warned us about --Jimmy Buffett
Padma Asrani
Honored Contributor

Re: How to Compare Files one by one using Shell Script

ok. People thanks a lot. I am trying all the options. Thanks so much to all.
Padma Asrani
Honored Contributor

Re: How to Compare Files one by one using Shell Script

Hi

Some more clarification. Both the directories have some files common and also files which are in one directory but not in other. How to get the list of uncommon files?

Secondly I am not sure i m not able to find dircmp on my linux box.

Thanks
Padma
Pete Randall
Outstanding Contributor

Re: How to Compare Files one by one using Shell Script

From the dircmp man page:

WARNINGS
This command is likely to be withdrawn from X/Open standards. Applications using this command might not be portable to other vendors' systems. As an alternative diff -R is recommended.

You might want to take a look at diff -R.


Pete

Pete
James R. Ferguson
Acclaimed Contributor
Solution

Re: How to Compare Files one by one using Shell Script

Hi Padma:

> Both the directories have some files common and also files which are in one directory but not in other. How to get the list of uncommon files?

Using 'diff' would show you this automatically. Consider:

# ls /tmp/dir1 /tmp/dir2

/tmp/dir1:
hosts inetd.conf

/tmp/dir2:
hosts services

# diff /tmp/dir1 /tmp/dir2
17c17
< 127.0.0.1 LOCALHOST loopback
---
> 127.0.0.1 localhost loopback
Only in /tmp/dir1: inetd.conf
Only in /tmp/dir2: services
#

...at worst, you could 'grep -v "^Only in"' to filter out the unmatched files; or 'grep "^Only in"' to find them!

> Secondly I am not sure i m not able to find dircmp on my linux box.

It probably isn't. In fact, the manpages for 'dircmp' note, "This command is likely to be withdrawn from X/Open standards. Applications using this command might not be portable to other vendors' systems. As an alternative diff -R is recommended."

Regards!

...JRF...
Padma Asrani
Honored Contributor

Re: How to Compare Files one by one using Shell Script

Thanks

it does work.

Regards
Padma
Padma Asrani
Honored Contributor

Re: How to Compare Files one by one using Shell Script

Hi

I was not able to find diff -R and I just used diff dir2> |grep "^Only in"

diff -R is not supported on my Linux box. I am using Fedora Linux.

Thanks
Padma
OldSchool
Honored Contributor

Re: How to Compare Files one by one using Shell Script

thanks....
James R. Ferguson
Acclaimed Contributor

Re: How to Compare Files one by one using Shell Script

Hi Padma:

> diff -R is not supported on my Linux box. I am using Fedora Linux.

No, but:

# diff -r

...and I'm running Fedora 8 in this case.

Your first line of defense is to do:

# man diff

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: How to Compare Files one by one using Shell Script

>Both the directories have some files common and also files which are in one directory but not in other. How to get the list of uncommon files?

You can just list both directories and use comm(3) to get this:
$ ls dir1 > dir_files.1
$ ls dir2 > dir_files.2
# Gives files only in dir1
$ comm -23 dir_files.1 dir_files.2
# Gives files only in dir2
$ comm -13 dir_files.1 dir_files.2
# Gives files that aren't in both:
# Ones in dir2 are indented
$ comm -3 dir_files.1 dir_files.2
TwoProc
Honored Contributor

Re: How to Compare Files one by one using Shell Script

Just an fyi -

I'm guessing you've realized it by now - but you're getting "HPUX" answers because you've posted in the HPUX forum. There is a Linux forum, in which you're much more likely to get Linux specific help.
We are the people our parents warned us about --Jimmy Buffett
James R. Ferguson
Acclaimed Contributor

Re: How to Compare Files one by one using Shell Script

Hi (again):

> but you're getting "HPUX" answers because you've posted in the HPUX forum

There was never a specification of what operating system but I think we have covered the options available to (L)UNIX at large.

Regards!

...JRF...
Rasheed Tamton
Honored Contributor

Re: How to Compare Files one by one using Shell Script

Hi Padma,

This one I mostly use:

for i in `find /tmp/dir1 -type f|awk -F/ '{print $NF}'`
do
find /tmp/dir2 -type f -exec ll {} \; |grep $i
done

Can be ported to any UNIX.

Regards,
Rasheed Tamton.