Last active
January 30, 2018 23:04
-
-
Save sbrl/8eeb849e8a170dce4fd8 to your computer and use it in GitHub Desktop.
[rmhash] Bash script to delete files if they have a hash equal to that of another file. #cli #tool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
if [ "$#" -lt 2 ] || [ "$#" -gt 3 ] ; then | |
echo Invalid number of parameters. This script takes 2 or 3 parameters. | |
echo | |
echo This script files files that have a hash equal to that of a specified | |
echo file and deletes them. | |
echo | |
echo =-=-=-=-=- | |
echo =- Help -= | |
echo -=-=-=-=-= | |
echo rmhash can be used in two ways. You can either specify a file to hash, | |
echo or you can specify a hash directly. | |
echo | |
echo To specify a file to hash, do something like this: | |
echo | |
echo \ \ rmhash.sh file \"filename here\" | |
echo | |
echo If you want to specify a hash, do something like this: | |
echo | |
echo \ \ rmhash.sh hash \"e834ccf7133275ad4b7f05d04c20f710\" | |
echo | |
echo If you don\'t want rmhash to delete anything, do something like this: | |
echo | |
echo \ \ rmhash file \"filename here\" notreally | |
echo | |
echo This script was built by Starbeamrainbowlabs on Windows 7. | |
echo Twitter: @SBRLabs Github: @sbrl Website: https://starbeamrainbowlabs.com/ | |
echo | |
echo Too much information? Try rmshash.sh \| less or rmhash.sh \| more. | |
echo | |
exit | |
fi | |
if [[ "$1" = "file" ]]; then | |
echo Entering file mode. | |
search_hash=$(md5sum "$2" | cut -c 1-32); | |
else | |
echo Entering hash mode. | |
search_hash=$2 | |
fi | |
delete=true | |
if [[ "$3" = "notreally" ]]; then | |
echo Not really deleting files. | |
delete=false | |
fi | |
count=0; | |
echo Searching for files with hash $search_hash. | |
echo | |
for file in **; do | |
[[ -f "$file" ]] || continue | |
hash=$(md5sum < ${file} | cut -c 1-32); | |
if [ "$hash" = "$search_hash" ]; then | |
echo $file | |
if [ "$delete" = true ]; then | |
rm $file | |
fi | |
((count++)) | |
fi | |
done | |
echo | |
if [ "$delete" = true ]; then | |
echo Found and deleted $count files. | |
else | |
echo Found $count files. | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment