0

I need to write a bash script. I have a folder name abc and the folder contains multiple (around 20) files. Now some of the files are named as __servicename__List.java Here __servicename is acting as a placeholder. I need to replace this placeholder with a string say xyz

Can someone please help? Appreciate all your help! Thanks in advance!

Sweta Sharma
  • 2,404
  • 4
  • 21
  • 36

1 Answers1

1

In bash I would do:

cd abc
for FileName in **/* ; do
    mv -- "$FileName" "${FileName/__servicename__/xyz}"
done
Benoit
  • 76,634
  • 23
  • 210
  • 236
  • Thank you for reply. I tried but somehow it didn't work. Did I miss something else? – Sweta Sharma Jun 03 '21 at 11:29
  • Its says ```mv: cannot stat '**/*': No such file or directory``` – Sweta Sharma Jun 03 '21 at 11:53
  • Are you using bash, or sh ? Also, are you in the right folder? Try only `*` maybe. – Benoit Jun 03 '21 at 14:05
  • I tires ```for FileName in * ; do mv -- "$FileName" "${FileName/_servicename_/$Service_Name}" done``` its still not working. Did I miss something? – Sweta Sharma Jun 03 '21 at 15:07
  • I tried ```$Service_Name="xyz" for FileName in * ; do mv -- "$FileName" "${FileName/_servicename_/$Service_Name}" done``` its still not working. Did I miss something? – Sweta Sharma Jun 03 '21 at 15:21
  • @SwetaSharma sorry for the late answer, but yes you missed somthing. In Bash, assignment to a variable is `var=content`, not `$var=content`. – Benoit Jun 23 '21 at 06:33