-1

I am trying to replace a list of filenames inside a directory. For example

cd /home/towers
ls

c3_slo_live_ox_dns_m2m_pcg.yaml
c3_slo_live_ox_dns_service_pcg_physnet4.yaml
c3_slo_live_ox_dns_service_pcg_physnet2.yaml

to

cd /home/towers
ls

c3_dsd_live_ox_dns_m2m_pcg.yaml
c3_dsd_live_ox_dns_service_pcg_physnet4.yaml
c3_dsd_live_ox_dns_service_pcg_physnet2.yaml

Which is the best way? can we use sed? Any example that I should try? 
Siddharth
  • 192
  • 3
  • 20

2 Answers2

0

If you what you wanted is to rename the files according a replacement pattern you could try something like this:

for file in *;do mv $file $(echo $file|sed 's/_slo_/_dsd_/'); done
  • This will loop through every file in your current directory and replace the _slo_ string with _dsd_

I recommend you double check that this pattern isn't used unintentionally in other parts of your filenames before you commit to it.

kiyell
  • 432
  • 2
  • 5
  • Probably try https://shellcheck.net/ on this before trying to use it; it contains several beginner mistakes. – tripleee Oct 28 '22 at 06:58
0

This might work for you:

rename 's/_slo_/_dsd_/' *

This uses the perl rename program.

potong
  • 55,640
  • 6
  • 51
  • 83