Compare files of 2 directories

Hi all,

I have 2 directories dir1 and dir2 which contains many xml files. I need to compare files of dir1 with that of dir2 and if they match, I need to cut it from dir1 and paste it in dir2. I need to do this thru scripts. I'm currently investigating on the diff command. Please help me write the script.

regards.

So you will end up having sets of two identical files - with different file names - in dir2? Any rules for creating new file names?

hi RudiC,

thnks.

The folders will not necessarily end up the same. I just want to take each file in dir1, check if it exists in dir2, if it exists, overwrite it by the file in dir1 and then remove it from dir1. If it does not exist, just don't do anything.

I need to do this in view of correcting the 2 folder contents because some program error has caused the folders to be in an incorrect state.

regards.

So you compare files' names, not files' contents? diff won't help, then. Make backups, and then, assuming your cwd is dir1, try (untested):

ls *.xml | while read FN; do [ -f /path/to/dir2/$FN ] && echo mv $FN /path/to/dir2/ ; done

Remove echo if happy with the result.

1 Like

hi RudiC,

thnks.
Your code works.

I was also able to do it in a bit similar to yours:

#!/bin/sh
cd /path_to/dir1
for a in `ls *.xml`; do
   if [ -f ../dir2/$a ]
   then
    echo $a >> filer1.txt
     mv $a ../dir2
   fi
done

regards.

You could use the -v option to mv to create a log.

Hi.

See post 9 in find same size file for a demonstration of removing duplicate files (by content).

Best wishes ... cheers, drl