Search code examples
shellunixscriptinghp-ux

Finding duplicate directories in separate directories in unix


I have 2 directories , dir1 and dir2.

There are some directories in them. Suppose dir1 has a,b and c directories and dir2 has c,d and e.

I need a script that will find out the duplicate directories in dir1 and dir2,in this case, "c" and log it into a separate file,say,duplicate.lst.


Solution

  • DIR1=dir1
    DIR2=dir2
    
    ## find all subdirectories of `dir1` by using subdir/. and loop
    for SUB in `cd $DIR1; echo */.`
    do
         ## for each check if it exists in $DIR2
         if [ -d $DIR2/$SUB ]; then
              echo "Duplicate: "`dirname $SUB`
         fi
    done