Question

I have identical looking but totally different 4 files.

> _palus_SLA_response_pages_main_2c3cd_2_18.tokudb
> _palus_SLA_response_pages_status_2c3cd_1_18.tokudb
> _palus_SLA_response_pages_main_51fdda_2_18.tokudb
> _palus_SLA_response_pages_status_51fdda_1_18.tokudb

I need to replace all the files having a code "51fdda" with the other one "2c3cd". So the new code will overwrite the files with old code and there will be only 2 files left:

_palus_SLA_response_pages_main_2c3cd_2_18.tokudb
_palus_SLA_response_pages_status_2c3cd_1_18.tokudb

I can move each file one at a time, something like this...

mv "_palus_SLA_response_pages_main_51fdda_2_18.tokudb" "_palus_SLA_response_pages_main_2c3cd_2_18.tokudb" 
mv "_palus_SLA_response_pages_status_51fdda_1_18.tokudb" "_palus_SLA_response_pages_status_2c3cd_1_18.tokudb" 

But there are too many files to be moved at a time manually. I need a script that will do this for me.

Was it helpful?

Solution

If you have the rename command available, you can use that:

rename 51fdda 2c3cd *tokudb

If you do not have the rename command available, you can do this with a simple loop:

for file in *51fdda*.tokudb; do
    mv ${file} ${file/51fdda/2c3cd}
done

For more information about this second solution, see the "Brace Expansion" section of the bash(1) man page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top