Batch rename files recursively, and number the directory index
With this code and your example data, both a/c/0.png and b/0.png get renamed to 1_0.png.  This happens because changes to $n inside ( ) get lost when you leave the subshell.

Use bash's "globstar" feature to recurse for you, and assign numbers as normal.  **/ matches all subdirectories.  You want the current directory too, so add . to the list.

This doesn't exactly match your example (A/C will be numbered before B) but it's close:

shopt -s globstar
n=0
for d in ./ **/ ; do
    ( 
        cd $d &amp;&amp; 
            for f in *.png; do 
                [[ -f $f ]] &amp;&amp; mv "$f" $n"_$f"
            done
    )
    (( n++ ))
done