Here's the use case:
I have an exported website that has images deeply nested throughout it in various folders. I want to find all of them, and copy them into a single directory. I don't care about the filenames so for the sake of both dedupliacting the results and making sure no filename collisions occur, I want to name the resulting images $(sha1sum $image).jpg
.
So I wrote a small script to do this and I felt like sharing:
shopt -s globstar # Enable `**` for globbing
shopt -s extglob # Enable extended glob matching
shopt -s nocaseglob # Enable case-insensitive globbing
mkdir -p out
for img in ./**/@(*.jpg|*.gif|*.jpeg|*.png); do
sha="$(sha1sum "$img")"
sum="${sha%%" "*}"
file="${sha##*" "}"
bn="$(basename "$file")"
ext="${bn##*.}"
out="./out/$sum.$ext"
echo "moving $file to $out"
mv "$file" "$out"
done