Monday, December 10, 2018

Find Differences Between Two Files Using Bash

grep -v -F -x -f file1 file2
The problem with the above command is that you dont know which way is the differences.  Here is a better way to accomplish this.  I have a directory of ID cards named MASTER_ID and is mounted remotely.  I host a local MYSQL database with all the accounts.  The problem we are trying to solve is move all the ID cards that are no longer in the database to the !Graveyard directory for consideration of permanently removal by a human.

if [[ $EUID -ne 0 ]]; then
 echo "This script must be run as root"
 exit 1
fi

MASTER_ID_DIR="/home/projects/mount_directory/srvr-file/SchoolPhotos/MASTER_ID/"
MASTER_ID_LST="/tmp/master_id.lst"
CAS_LST="/tmp/cas.lst"

# Generate list of all users within CAS by ID
if [ -f $CAS_LST ]; then
 rm $CAS_LST
fi
mysql -u cas_god -p <dbpasswd> -D cas -e "select id_number from cardinfo_cardinfo into outfile '/tmp/cas.lst'"

# Get listing of MASTER_ID regardless of the file extension case
echo "" > $MASTER_ID_LST
for x in `ls $MASTER_ID_DIR`; do
 echo $(basename $x .[Jj][Pp][Gg]) >> $MASTER_ID_LST
done

# Find extras in MASTER_ID that is NOT in CAS
for y in `cat $MASTER_ID_LST`; do
 if [ `grep $y $CAS_LST|wc -l` -eq 0 ]; then
   echo mv $MASTER_ID_DIR${y}.* $MASTER_ID_DIR!Graveyard/${y}_delete.jpg
   mv $MASTER_ID_DIR${y}.* $MASTER_ID_DIR!Graveyard/${y}_delete.jpg
 fi
done