How do I change the extension of multiple files?
Straight from Greg's Wiki:
# Rename all *.txt to *.text for f in *.txt; do mv -- "$f" "${f%.txt}.text" done
Also see the entry on why you shouldn't parse ls
.
Edit: if you have to use basename
, your syntax would be:
for f in *.txt; do mv -- "$f" "$(basename -- "$f" .txt).text" done
Here's how I change all the file extensions in the current directory on Debian.
rename "s/oldExtension/newExtension/" *.txt
(This is the Perl rename
command, not the util-linux
one. See Why is the rename utility on Debian/Ubuntu different than the one on other distributions, like CentOS?)
A simple command, the rename
from util-linux
, will do that for you, it replace every occurences of "txt" to "text" in all file matching "*.txt":
rename txt text *.txt