0

I need to write a bash script. A folder can have multiple folders. Let's say the parent folder is ABC which has subfolders. Now all the folders including the subfolders one has a filename with a placeholder in it example _service_log.java I need to replace the placeholder with a variable that has a value in it say ZYX in all the folders.

the output should be like ZXYlog.java

I tried the below solution However It's not working

$Service_Name="ZXY"
cd abc
    for FileName in * ; do
        mv -- "$FileName" "${FileName/_servicename_/$Service_Name}"
    done

Can someone please help?

Appreciate all your help! Thanks in advance!

Sweta Sharma
  • 2,404
  • 4
  • 21
  • 36
  • "*I need to write a bash script.*" // "*Can someone please help?*" ---> Please [edit] your question to show us what you've tried, where you got stuck. We can help, but SO isn't a free coding service. – 0stone0 Jun 03 '21 at 13:34
  • In case you can't infer the code needed to achieve this from the duplicate target, here is the spoonfed answer: `find . -name '_service_*' -type f -exec sh -c 'for f; do mv "$f" "${f%/*}/ZXY${f##*/_service_}"; done' sh {} +` – oguz ismail Jun 03 '21 at 14:07
  • I have added my solution in the question. However It is not working could you please help – Sweta Sharma Jun 03 '21 at 15:17
  • @oguzismail I have added the solution in the question. Can you help me with the missing part – Sweta Sharma Jun 03 '21 at 15:29

0 Answers0