I'm trying to rename several directories adding dashes after fourth and sixth characters. I looked at this post How do I loop through only directories in bash? and was able to successfully loop through the directories that I wish to insert the dashes into. However, after seeing this Rename files add dashes after fourth and sixth characters I though perhaps I could use similar syntax on directories, but after doing so realized it can't be done that way. This was my syntax:
for d in */ ; do
mv “$d" “${d::4}-${d:4:2}-${d:6}"
done
I searched through SE and didn't see another post like this. I apologize in advance if this question is answered elsewhere.
Also, thank you in advance for any help you might offer. Best, Jon
d=abcdefghijkl; echo "${d::4}-${d:4:2}-${d:6}"
givesabcd-ef-ghijkl
. Though you have some curly quotes there, and they won't work right in the shell. Make sure to change them to ASCII quotes. The renaming loop looks fine. (Though you might want to usemv -n
to prevent trashing any files that happen to exist under the new names) – ilkkachu Dec 19 '22 at 20:51