Operating System - HP-UX
1834772 Members
3153 Online
110070 Solutions
New Discussion

Comparing directory trees

 
SOLVED
Go to solution
Peter Kempner
Occasional Contributor

Comparing directory trees

I want to look at 2 similar, large, deep directory trees and identify all common files

I think I need to use ll and comm commands but am stuck because of the way ll lists files in a directory tree. Instead of listing every file in the tree along with it's directory on one line, it lists the files under headings which specify the directory.

This makes the files impossible to compare!

Anyone ever had to sort this kind of problem or got any bright ideas???
8 REPLIES 8
DIPAK KUMAR ROY
Frequent Advisor

Re: Comparing directory trees

Try "dircmp" command.

Thanks
James A. Donovan
Honored Contributor

Re: Comparing directory trees

dircmp -s
Remember, wherever you go, there you are...
Pete Randall
Outstanding Contributor

Re: Comparing directory trees

Peter,

I'm not sure whether this would help you, but have you looked at the find command? It will chase down through the directory trees for you, printing out the names of the files as it finds them, or you can ask it to perform a command on each file as it finds them (like ls -l, for example: find /start_dir -exec ll {} \;). Do a man on "find" and take a look at the examples.


Pete

Pete
John Palmer
Honored Contributor
Solution

Re: Comparing directory trees

Do you want to identify common files in common subdirectories?

If so then something like this may do:

cd dir1
find . | sort > tempfile1
cd dir2
find . | sort > tempfile2
comm -12 tempfile1 tempfile2
rm tempfile1 tempfile2

find will print every file as a relative pathname so you should be able to compare them.
Elena Leontieva
Esteemed Contributor

Re: Comparing directory trees

Try this:

cd /dir1
find . -type f -print |sort > /tmp/dir1
cd /dir2
find . -type f -print |sort > /tmp/dir2
comm -3 /tmp/dir[12]
rm /tmp/dir[12]

Elena.
Michael Schulte zur Sur
Honored Contributor

Re: Comparing directory trees

Hi,

try this:

#!/bin/ksh
find /dir1 -type f -exec ls -l '{}' ';' | awk -f t11.awk > files1
find /dir2 -type f -exec ls -l '{}' ';' | awk -f t11.awk > files2
comm -3 files1 files2

# cat t11.awk
BEGIN{FSX=FS;}
{
for (i=1; i<=NF-1; i++)
printf("%s ",$i);
FS="/";
printf("%s\n",$NF);
FS=FSX;
}
END{}

greetings,

Michael
Leif Halvarsson_2
Honored Contributor

Re: Comparing directory trees

Hi,
I would suggest you to try the join command.
First create one file for each directory tree with "find . -print >". Then sort the files (sort command).
Last, "join outfile1 outfile2" which produces an output for each matching line. There is several options for join (for example not matching lines), have a look at the man page.

The difference with join compared to comm is that join worka also when there is not equal numbere of lines in the files and with 1 to many relations.
Sandro_8
New Member

Re: Comparing directory trees

I'm looking for Mr Peter Kempner,

Lived in Northwood Middlesex in Westbury Road. Is that you?

This is Sandro, the itlaian at jamesaltt@yahoo.it, trying to get in contact with you.

If details do not match, ignore this note

Bye

Sandro