Locate and delete file copies

Last Updated or created 2022-12-28

A simple script to locate files and select which to delete.
Uses md5sum to compare
(just be sure locatedb is up to date!)

./locatemd5 VID_20130926_211302.mp4
1 : 125a65e830c1f3654714daa0f8a41699  /tank/Backup/Nae Bother Drive/Movies/VID_20130926_211302.mp4
2 : 125a65e830c1f3654714daa0f8a41699  /tank/Backup/rclonegdrive/Nae Bother Drive/Movies/VID_20130926_211302.mp4
3 : 125a65e830c1f3654714daa0f8a41699  /tank/Private/Henri/_All online drives/Google Drive Henri/Nae Bother Drive/Movies/VID_20130926_211302.mp4
4 : 125a65e830c1f3654714daa0f8a41699  /tank/Private/gfx/Gsm/GSM Henri/nexus/2013/20130926/VID_20130926_211302.mp4
5 : 125a65e830c1f3654714daa0f8a41699  /tank/Private/Work Directory/Sorted/movies/own/VID_20130926_211302.mp4
6 : 82cd340b2b54d3ef65a02c8f31b04970  /tank/Private/www/cutshort/VID_20130926_211302.mp4
---------- which to delete ---------------
1 2 3 5
delete 1 /tank/Backup/Nae Bother Drive/Movies/VID_20130926_211302.mp4
rm: remove regular file '/tank/Backup/Nae Bother Drive/Movies/VID_20130926_211302.mp4'? y
delete 2 /tank/Backup/rclonegdrive/Nae Bother Drive/Movies/VID_20130926_211302.mp4
rm: remove regular file '/tank/Backup/rclonegdrive/Nae Bother Drive/Movies/VID_20130926_211302.mp4'? y
delete 3 /tank/Private/Henri/_All online drives/Google Drive Henri/Nae Bother Drive/Movies/VID_20130926_211302.mp4
rm: remove regular file '/tank/Private/Henri/_All online drives/Google Drive Henri/Nae Bother Drive/Movies/VID_20130926_211302.mp4'? y
delete 5 /tank/Private/Work Directory/Sorted/movies/own/VID_20130926_211302.mp4
rm: remove regular file '/tank/Private/Work Directory/Sorted/movies/own/VID_20130926_211302.mp4'? y

Bash Script

#!/bin/bash
# below gets array from subshell
shopt -s lastpipe

nr=1
locate "$1" | while read file ; do
        md5sum=$(md5sum "$file")
        echo  "$nr : $md5sum"
        myArray[$nr]="$file"
        (( nr++ ))
done
echo  "---------- which to delete ---------------"
read numbers
for f in $numbers ; do
 echo "delete $f ${myArray[$f]}"
 rm -i "${myArray[$f]}"
done
Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *