2

I am comparing multiple files from a directory. Among the files some are matched some are mismatched. I am able to output the difference using diff command. I want to email the mismatched files as attachments.

#!/bin/bash

DIR1=/usr/local/apps/B2B/filecompare/inf
DIR2=/usr/local/apps/B2B/filecompare/gen/genB

DEST_DIR1=/usr/local/apps/B2B/filecompare/moved/cmpinf
DEST_DIR2=/usr/local/apps/B2B/filecompare/moved/cmpgen

export RESULT=$1

for FILE1 in `ls $DIR1`
do
    if [ ! -f $DIR2/$FILE1 ]; then
        echo "  $FILE1 exists in $DIR1 but not found in $DIR2. Skipping file." >> $RESULT
    else
        echo "  Comparing $DIR1/$FILE1 $DIR2/$FILE1." >> $RESULT
        diff -B $DIR1/$FILE1 $DIR2/$FILE1 >> $RESULT
        #diff $DIR1/$FILE1 $DIR2/$FILE1 >>RESULT
        echo "  Done comparing $DIR1/$FILE1 $DIR2/$FILE1." >> $RESULT

        mv $DIR1/$FILE1 $DEST_DIR1
        mv $DIR2/$FILE1 $DEST_DIR2
        echo "  Done moving files" >> $RESULT

    fi
done

for FILE2 in `ls $DIR2`
do
    if [ ! -f $DIR1/$FILE2 ]; then
        echo "  $FILE2 exists in $DIR2 but not found in $DIR1. Skipping file." >> $RESULT
    fi
done

echo "  Done comparing." >> $RESULT
zip -r $DEST_DIR1/downlaod.zip $DEST_DIR1/$FILE1 $DEST_DIR2/$FILE1
mail -a $DEST_DIR1/downlaod.zip xvz@abc.com

Thank you

  • Your script seems to start in the middle of something. – muru Mar 6 '17 at 5:51
  • I posted my entire script – s12 Mar 6 '17 at 5:55
  • Shouldn't you be running the zip command inside the first loop? – muru Mar 6 '17 at 6:00
  • Okay. But how will I make sure that the zip command will zip the mismatched files only? – s12 Mar 6 '17 at 6:14
  • how are you making sure you're only moving and listing the mismatched files now? – muru Mar 6 '17 at 6:15
0

This should work. It builds the list of differing files while cycling through the first loop and then uses a different zip command to zip them up.

#!/bin/bash

DIR1=/usr/local/apps/B2B/filecompare/inf
DIR2=/usr/local/apps/B2B/filecompare/gen/genB

DEST_DIR1=/usr/local/apps/B2B/filecompare/moved/cmpinf
DEST_DIR2=/usr/local/apps/B2B/filecompare/moved/cmpgen

export RESULT=$1
touch different.txt

for FILE1 in `ls $DIR1`
do
    if [ ! -f $DIR2/$FILE1 ]; then
        echo "  $FILE1 exists in $DIR1 but not found in $DIR2. Skipping file." >> $RESULT
    else
        echo "  Comparing $DIR1/$FILE1 $DIR2/$FILE1." >> $RESULT
        diff -B $DIR1/$FILE1 $DIR2/$FILE1 >> $RESULT
        #diff $DIR1/$FILE1 $DIR2/$FILE1 >>RESULT
        echo "  Done comparing $DIR1/$FILE1 $DIR2/$FILE1." >> $RESULT

        # NEW: register if there is a diff
        is_diff=$(diff -q $DIR1/$FILE1 $DIR2/$FILE2 >/dev/null; echo $?)

        # NEW: build a list of the files that are different
        if [ "$is_diff" -gt 0 ]; then
            echo $DEST_DIR1/$FILE1 >> different.txt
            echo $DEST_DIR2/$FILE1 >>  different.txt
        fi


        mv $DIR1/$FILE1 $DEST_DIR1
        mv $DIR2/$FILE1 $DEST_DIR2
        echo "  Done moving files" >> $RESULT

    fi
done

for FILE2 in `ls $DIR2`
do
    if [ ! -f $DIR1/$FILE2 ]; then
        echo "  $FILE2 exists in $DIR2 but not found in $DIR1. Skipping file." >> $RESULT
    fi
done

echo "  Done comparing." >> $RESULT
# NEW: zip the file list of differing files
zip $DEST_DIR1/download.zip -@ < different.txt
mail -a $DEST_DIR1/download.zip xvz@abc.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.