how to change the extension of multiple files using bash script? -
i new linux usage maybe first time hope detailed please. have more 500 files in multiple directories on server (linux) want change extensions .xml using bash script used lot of codes none of them work codes used :
for file in *.txt mv ${file} ${file/.txt}/.xml done
or
for file in *.* mv ${file} ${file/.*}/.xml done
i not know if second 1 valid code or not tried change txt extension beacuse prompt said no such file '.txt'
i hope thank you
explanation
- for recursivity need bash
>=4
, enable**
(i.e.globstar
) ; - first, use parameter expansion remove string
.txt
, must anchored @ end of filename (%
) : - the
#
anchors pattern (plain word or glob) beginning, - and
%
anchors end. - then append new extension
.xml
- be cautious filename, should always quote parameters expansion.
code
this should in bash
(note echo
the old/new filename) :
shopt -s globstar # enable ** globstar/recursivity in **/*.txt; [[ -d "$i" ]] && continue # skip directories echo "$i" "${i/%.txt}.xml"; done
Comments
Post a Comment