 <p>I'd use <code>find</code> + <code>egrep</code> to filter, then <code>sed</code> to build the name of the destination directory. </p> <pre><code>cd /src IMAGES=`find . -type f -name '*.png' -print | egrep '^./[0-9]{4}-[0-9]{2}-[0-9]{2}-.+.png$'` for IMG in $IMAGES; do # optimize here DIR=`echo $IMG | sed -E 's/^\.\/([0-9]{4})-([0-9]{2})-[0-9]{2}-.+.png/\1\/\2/'` mkdir -p /dest/$DIR mv /src/$IMG /dest/$DIR/ done </code></pre>