Command / script to partially rename file

Hi
I have numerous files names

product_host_result_B1000842.txt
product_host_result_B1000847.txt
product_host_result_C1000842.txt
product_host_result_C1000848.txt

etc. I need them renamed so that the 'product_host_result' becomes 'output_product_host' but the rest of the filename is retained (I've given some examples but it could be almost anything!)
Any help much appreciated

If you have rename utility

rename product_host_result output_product_host product_host_result*.txt

Please test first.

1 Like

Would that work?

ls product_host_result_*.txt | xargs -n1 -I {} mv -v {,output_}{}

Try:

for i in *; do mv $i output_product_host_${i##*_}; done

If you don't get any joy with that, can you show us what you want to convert from and what you want to convert to?

I just tried rename as you posted to check it and it didn't do anything. So I changed to this,

rename 's/product_host_result/output_product_host/' product_host_result*.txt

and this worked to me.

I am using Bash in Linux and you may be using something different.

1 Like

I tried this one and it renamed every file in the directory ! Luckily I tested it in my $home first !

Apologies should have clarified this is a shared filesystem - not just the ones I want to rename..

---------- Post updated at 10:36 AM ---------- Previous update was at 10:35 AM ----------

Hi clx

This doesn't work for me. The rename doesn't return any errors so I don't know why..