BASH script to rename XML file to an attribute value

I have a lot of .xml files structured the same way:

<parent id="idvalue" attr1="val1" attr2="val2" ...>
    <child attr3="val3" attr4="val4" ... />
    <child attr3="val5" attr4="val6" ... />
    ...
</parent>

Each file has exactly one <parent> element with exactly one id attribute.

All of those files (almost 1,700,000 of them) are named as part.xxxxx where xxxxx is a random number.

I want to name each of those files as idvalue.xml, according to the sole id attribute from the file’s content.

I believe doing it with a bash script would be the fastest and most automated way. But if there are other suggestions, I would love to hear them.

My main problem is that I am not able (don’t know how) to get the idvalue in a specific file, so that I could use it with the mv file.xxxxx idvalue.xml command.


Source: bash

Leave a Reply