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

  1. for recursivity need bash >=4 , enable ** (i.e. globstar) ;
  2. first, use parameter expansion remove string .txt, must anchored @ end of filename (%) :
  3. the # anchors pattern (plain word or glob) beginning,
  4. and % anchors end.
  5. then append new extension .xml
  6. be cautious filename, should always quote parameters expansion.

code

this should in bash (note echothe old/new filename) :

shopt -s globstar # enable ** globstar/recursivity in **/*.txt;     [[ -d "$i" ]] && continue # skip directories     echo "$i" "${i/%.txt}.xml"; done 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -