How do i change the extension of multiple files?
I would like to change a file extension from *.txt
to *.text
. I tried using the basename
command, but I'm having trouble on changing more than one file.
So here is my code
files=`ls -1 *.txt`
for x in $files
do
mv $x "`basename $files .txt`.text"
done
I'm getting this error
basename: too many arguments Try basename --help' for more information
Best Answer
Straight from Greg's Wiki .
# Rename all *.txt to *.text
for f in *.txt; do
mv -- "$f" "${f%.txt}.text"
done
*.txt
is a globbing pattern , using *
as a wildcard to match any string. *.txt
matches all filenames ending with '.txt'.
--
marks the end of the option list . This removes problems with filenames starting with hyphens
${f%.txt}
is a parameter expansion , replaced by the value of the f
variable with .txt
removed from the end.
Also see the entry on why you shouldn't parse ls
.
If you have to use basename
, your syntax would be.
for f in *.txt; do
mv -- "$f" "$(basename -- "$f" .txt).text"
done