- Don't parse
ls
- Don't use string concatenation to format numbers. Use
printf
- Quote your variables
Your script can be written as:
c=0
for i in *.mp3; do
c=$((c+1))
pref=$(printf "%03d" $c)
echo "$i" "${pref}|$i"
done
If the output seems good, replace echo
with mv
to perform the actual rename.