Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

diff between 2 directory

Status
Not open for further replies.

hokky

Technical User
Nov 9, 2006
170
AU
Hi Guys,

I'm trying to "diff" between 2 directories.
And I wanna get the returns which files is different

I've tried :

diff -r dir1 dir2

but the result is too much, I got every single characters different in the file. But I just need which files. That's all.

I've looked in man diff but no luck

Thanks guys,
 
use cmp command inside a for loop

something like this (untested):

cd /path/to/dir1
for file in $(ls)
do
test -f $file && cmp $file /path/to/dir2/$file
done


HTH,

p5wizard
 
Hmm

There are two different things here
[ol]
[li]for each file in dir1 does it exist in dir2, and vice versa[/li]
[li]for each file in both dirs, is it the same file[/li]
[/ol]
For the first half
Code:
#Make a list of files in dir1
cd /path/to/dir1
find . -type f > /tmp/dir1.files.$$

#Make a list of files in dir2
cd /path/to/dir2 
find . -type f > /tmp/dir2.files.$$
#Compare the lists
diff /tmp/dir1.files.$$ /tmp/dir2.files.$$
For the second half
Code:
#Merge the lists into one
sort -u /tmp/dir1.files.$$ /tmp/dir2.files.$$ > /tmp/all.files.$$
#Iterate through the list
while read file
do
  #Reject any which are not in both dirs
  [[ -f $/path/to/dir1/$file && -f /path/to/dir2/$file ]] || continue;
  #Test for differences and report
  diff $/path/to/dir1/$file /path/to/dir2/$file >/dev/null && echo $file exists in dir1 and dir2 and is different
done < /tmp/all.files.$$
#Tidy up
rm /tmp/all.files.$$ /tmp/dir1.files.$$ /tmp/dir2.files.$$
Note that this is not tested

Ceci n'est pas une signature
Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top