0

How can I do the following change in file names. I suppose this is possible with rename, but I do not know how.

A_B_C.pdf

to

C_A_B.pdf

A, B and C are different words in my file names, like Complete_Works_Plato.pdf, for example.

1 Answer 1

1
rename -n 's/([^_]*_[^_]*)_([^_]*).pdf/$2_$1.pdf/' *
  • [^_]* Any number of non-underscores.
  • ( ) Captures a group. The 1st pair is reproduced by $1 and the 2nd pair by $2.

Remove the -n if you are satisfied with the output.

A similar approach with shell script:

for file in *.pdf; do
    f12=${file%_*}
    f3=${file##*_}
    f3=${f3%.pdf}
    mv -- "$file" "${f3}_${f12}.pdf"
done

In parameter expansion you find the meaning of % and ##.

2
  • 1
    Perfect! Thanks too for the info for doing a shell script.
    – fich
    Aug 12, 2020 at 13:40
  • @fich You are welcome. I provided the shell script because some systems may lack rename.
    – Quasímodo
    Aug 12, 2020 at 23:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .