How to rename many files with string substitution

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the bash category.

Last Updated: 2024-03-05

I had a bunch of files ending with .followers (corresponding to my various twitter accounts).

I wanted to change their endings to .txt. After Googling, I arrived at the following formulation, which nicely demonstrates string substitution in bash:

for file in *.followers; 
do; 
  mv $file ${file/%.followers/.txt}; 
done;

What does this % do specifically (${string%substring})? It deletes shortest match of $substring from back of $string. I.e. here I had ${$file/%.followers} so it removed the .followers bit.