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
You can do it using the Perl-based
rename
I think, using a mix of non-greedy and greedy matches ex.The non-greedy modifier on the last (
date
) group isn't really necessary - but I like the symmetry.Alternatively, with the shell alone