Skip to content

Instantly share code, notes, and snippets.

@imsakg
Last active April 9, 2023 00:38
Show Gist options
  • Save imsakg/09033ccbb93216a4d2b42086bec429cc to your computer and use it in GitHub Desktop.
Save imsakg/09033ccbb93216a4d2b42086bec429cc to your computer and use it in GitHub Desktop.
Simple, easy to use and powerful shell script. That detects same file with same hash and removes one of them.
#!/bin/bash
# The script will detect copy files from current directory (sha512hash) and remove copy ones with prompt.
# Usage: ./rm_multi_files.sh
files=$(ls -1)
files_count=$(ls -1 | wc -l)
echo "Total files: $files_count"
# echo "Files: $files"
# Loop through files count
for ((i = 1; i < $files_count; i++)); do
# Get file name
file_1=$(echo "$files" | sed -n "$i"p)
if [ -f "$file_1" ]; then
# Get file hash
file_1_hash=$(sha512sum "$file_1" | awk '{print $1}')
# Loop through files count
for ((j = i + 1; j < $files_count; j++)); do
# Get file name
file_2=$(echo "$files" | sed -n "$j"p)
if [ -f "$file_2" ]; then
# Get file hash
file_2_hash=$(sha512sum "$file_2" | awk '{print $1}')
# Check if file hash is same
if [ "$file_1_hash" == "$file_2_hash" ]; then
echo $file_1_hash $file_2_hash
# user prompt
echo "file_1 : $file_1"
echo "file_2 : $file_2"
echo "Are same. Please press 1 for deleting file_1 and 2 for file_2(2) also you can (p)ass or (e)xit ? (1/2/p/e)"
read -r answer
if [ "$answer" == "1" ]; then
echo "Removing $file_1"
rm "$file_1"
elif [ "$answer" == "2" ]; then
echo "Removing $file_1"
rm "$file_1"
elif [ "$answer" == "p" ]; then
continue
else
exit
fi
fi
fi
done
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment