I need to delete files from a directory by looking at Md5 checksum values present in lookup file.

Lookup file values - md5lkp.txt

aac2050c8f3a0b6de33c6a0976a77c77  TEST_20190505060848.csv
c36c63d1b0dcf0a34a6285597ae67e93  TEST_20190505130157.csv
e72caf404c85d712971fa5b491204a58  TEST_20190506060803.csv
6c8dba8210a7df31aecd8b08c015919e  TEST_20190506130210.csv
782f1233c6e0a7763e41dd372834253f  TEST_20190507061001.csv

Files in dir - /home/usr1

TEST_20190505060848.csv
TEST_20190505060850.csv
TEST_20190506130210.csv
TEST_20190506130212.csv

From above list below 2 file should get deleted

TEST_20190505060848.csv
TEST_20190506130210.csv

Please advise.

Able to Solve this as below :


#!/bin/bash

lkpfile=$1

for i in *.csv
do
        md5check=`md5sum $i | cut -d' ' -f1`
        echo "current file $i md5 is $md5check"
        delfile=`grep -r $md5check $lkpfile`
        echo "delfile : $delfile"
        delfilename=`echo $delfile|awk -F' ' '{print $2}'`
        echo "deleting : $delfilename"
        rm $delfilename
done

2 Answers

0
Community On

try this:

#!/bin/bash
mapfile -t hashs < <(cut -d " " -f1 md5ljp.txt)
while read -r file; do
    sum=$(md5sum "$file")
    [[ ${hashs[*]} =~ ${sum%% *} ]] && rm "$file"
done < <(ls ./*.csv)
0
Arvind Singh Kamlakar On

Able to achive this as below

#!/bin/bash

lkpfile=$1

for i in *.csv
do
        md5check=`md5sum $i | cut -d' ' -f1`
        echo "current file $i md5 is $md5check"
        delfile=`grep -r $md5check $lkpfile`
        echo "delfile : $delfile"
        delfilename=`echo $delfile|awk -F' ' '{print $2}'`
        echo "deleting : $delfilename"
        rm $delfilename
done