Problem
I have thousands folders, which names are structured like:
firstName_lastName_date
for which I want to swap the two first parts using the underscore _
as a separator in order to end up with:
lastName_firstName_date
Warning
lastName
may be be preceded by a nobiliary particle, separated with an extra _
in the input folders.
For example:
firstName_de_lastName_date
firstName_von_lastName_date
For which the wanted output should be:
de_lastName_firstName_date
von_lastName_firstName_date
Information
date
is always the last element.- First names are single words.
- Last names are single words or composed words (2) with a particle.
Tools
I want to do that using rename
in bash, preferably, or shell Parameter expansion.
References
https://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html
Jose_Maria_de_los_lobos_y_garcia_date
? Or can we be sure there will always be either 3 or 2_
and whatever is between the 1st and last_
will be the last name?