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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityrename -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 ##
.
rename
.
Aug 12, 2020 at 23:44