I am very new with linux usage maybe this is my first time so i hope some detailed help please. I have more than 500 files in multiple directories on my server (Linux) I want to change their extensions to .xml using bash script I used a lot of codes but none of them work some codes i used :
for file in *.txt
do
mv ${file} ${file/.txt}/.xml
done
or
for file in *.*
do
mv ${file} ${file/.*}/.xml
done
i do not know even if the second one is valid code or not i tried to change the txt extension beacuse the prompt said no such file '.txt'
I hope some good help for that thank you
>=4
and to enable **
(i.e. globstar
) ;.txt
, which must be anchored at the end of the filename (%
) :#
anchors the pattern (plain word or glob) to the beginning, %
anchors it to the end..xml
This should do it in Bash
(note that I only echo
the old/new filename, to actually rename the files, use mv
instead of echo
) :
shopt -s globstar # enable ** globstar/recursivity
for i in **/*.txt; do
[[ -d "$i" ]] && continue; # skip directories
echo "$i" "${i/%.txt}.xml";
done
If its a matter of a one or two sub-directories, you can use the rename command:
rename .txt .xml *.txt
This will rename all the .txt to .xml files in the directory from which the command is executed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With