File: shame512.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright (c) 2026 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 options are, available both in single and double-dash versions 35 # 36 # -h, -help show this help message 37 38 39 case "$1" in 40 -h|--h|-help|--help) 41 awk '/^# +shame512 /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 42 exit 0 43 ;; 44 45 -) ;; 46 47 --) shift ;; 48 49 -*) 50 printf "unsupported option '%s'\n" "$1" >&2 51 exit 1 52 ;; 53 esac 54 55 if [ $# -eq 0 ]; then 56 awk '/^# +shame512 /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2 57 exit 1 58 fi 59 60 for arg in "${@:-.}"; do 61 if [ -f "${arg}" ]; then 62 printf "%s\n" "${arg}" 63 continue 64 fi 65 66 if [ -d "${arg}" ]; then 67 if echo "${arg}" | grep -q '/$'; then 68 find "${arg}" -type f 69 else 70 find "${arg}/" -type f 71 fi 72 fi 73 done | awk -v ORS='\000' '!names[$0]++' | xargs -0 sha512sum | awk ' 74 BEGIN { if (SUBSEP == "") SUBSEP = "\034" } 75 76 { 77 hash = $1 78 name = $0 79 gsub(/^[^ ]+ *| *\r?$/, "", name) 80 81 if (!(hash in names)) hashes[++nhashes] = hash 82 names[hash SUBSEP (++nitems[hash])] = name 83 } 84 85 END { 86 for (i = 1; i <= nhashes; i++) { 87 k = hashes[i] 88 n = nitems[k] 89 90 if (i > 1) print "" 91 print k 92 for (j = 1; j <= n; j++) print names[k SUBSEP j] 93 } 94 } 95 '