How do I rename the extension for a bunch of files?

bmw0128 picture bmw0128 · Aug 3, 2009 · Viewed 346.7k times · Source

In a directory, I have a bunch of *.html files. I'd like to rename them all to *.txt

How can I do that? I use the bash shell.

Answer

ghostdog74 picture ghostdog74 · Aug 4, 2009

If using bash, there's no need for external commands like sed, basename, rename, expr, etc.

for file in *.html
do
  mv "$file" "${file%.html}.txt"
done