File: shame512.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 2025 pacman64 6 # 7 # Permission is hereby granted, free of charge, to any person obtaining a copy 8 # of this software and associated documentation files (the “Software”), to deal 9 # in the Software without restriction, including without limitation the rights 10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 # copies of the Software, and to permit persons to whom the Software is 12 # furnished to do so, subject to the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be included in 15 # all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 # SOFTWARE. 24 25 26 # shame512 [options...] [files/folders...] 27 # 28 # Group files by common/same SHA-512 hashes: having the same hash means a high 29 # chance of having the same contents, even though that's not a guarantee. 30 # 31 # Files and folder names can be given together for convenience: folder names 32 # are searched recursively to find all files, including in any subfolders. 33 # 34 # The only option available is to show this help message, using any of 35 # `-h`, `--h`, `-help`, or `--help`, without the quotes. 36 37 38 case "$1" in 39 -h|--h|-help|--help) 40 awk '/^# +shame512 /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 41 exit 0 42 ;; 43 esac 44 45 [ "$1" = "--" ] && shift 46 47 if [ $# -eq 0 ]; then 48 awk '/^# +shame512 /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2 49 exit 1 50 fi 51 52 for arg in "${@:-.}"; do 53 if [ -f "${arg}" ]; then 54 printf "%s\n" "${arg}" 55 continue 56 fi 57 58 if [ -d "${arg}" ]; then 59 if echo "${arg}" | grep -q '/$'; then 60 stdbuf -oL find "${arg}" -type f 61 else 62 stdbuf -oL find "${arg}/" -type f 63 fi 64 fi 65 done | awk -v ORS='\000' '!names[$0]++' | xargs -0 sha512sum | awk ' 66 { 67 hash = $1 68 name = substr($0, length($1) + 1) 69 gsub(/^ *| *\r?$/, "", name) 70 71 if (!(hash in names)) hashes[++num_hashes] = hash 72 names[hash][length(names[hash]) + 1] = name 73 } 74 75 END { 76 for (i = 1; i <= num_hashes; i++) { 77 if (i > 1) print "" 78 k = hashes[i] 79 n = length(names[k]) 80 print k 81 for (j = 1; j <= n; j++) print names[k][j] 82 } 83 } 84 '