#!/bin/bash for collection in */ ; do for size in "$collection"*/ ; do for image in "$size"* ; do if [ -f "$image" ]; then fileName=$(basename "$image") imageName="${fileName%.*}" hash=$(echo -n "$imageName" | sha1sum | awk '{print $1}') path=$(dirname "$image")/${hash:2:2}/${hash:0:2} mkdir -p $path ln -s "$image" "$fileName" mv $image $path/$fileName fi done done done