I need to batch rename images with Mac Bash,while the directory index should be recorded.
To rename files ,turn
Root---A----0.png
| |
| ----1.png
|
-------B----0.png
|
----1.png
to
Root---A----0_0.png
| |
| ----0_1.png
|
-------B----1_0.png
|
----1_1.png
Here is my code, it is ok to deal with the situation that dir
A has a name of spacing.
Base=$(pwd)
num=0
IFS='
' # split on newline only
for path in *
do
if [[ -d $path ]]; then
NewPath="${Base}/${path}"
for f in "$NewPath"/*
do
dir=`dirname "$f"`
base=`basename "$f"`
name="${dir}/${num}_${base}"
mv ${f} "$name"
done
num=$((num + 1))
fi
done
Any way to do it more brilliantly? find
is a good option to handle files recursively.