You are not logged in.
i have a bunch of .pdf and .docx files which i'd like to batch rename. i tried to utilize the "rename" command for this task and since i'm using it for the first time and regex is not my strength this lead to nothing. i searched the internet and the commands i found didn't work either. this is how the commands looked like:
rename 's/^.{0,16}(_?)(.*)\.pdf/$3$2$1$4.pdf/' 'companyxy-6do5zb_Project Name.pdf'
rename '^.{0,16}(_?)(.*)\.pdf/$3$2$1$4.pdf/' 'companyxy-6do5zb_Project Name.pdf'
rename 's/^.({0,16})(_?)(.*)\.pdf' '$/$3$2$1.pdf/' 'companyxy-6do5zb_Project Name.pdf'
... and similar
the files to be renamed look like this:
companyxy-6do5zb_Project Name.pdf
and i want them to be like:
Project Name_companyxy-6do5zb.pdf
companyxy ... has always 9 characters and doesn't change
- ... is always there after companyxy
6do5zb ... always 6 random numbers and letters
_ ... is always there after the 6 random numbers and letters
Project Name ... can be anything, can contain numbers and special characters and can get quite long sometimes
.pdf ... the extension can also be .docx
can somebody please explain how to achieve this?
Offline
I've never used "rename", but here's an approach without it. In the directory with those files, run the following:
for f in *.pdf *.docx; do echo mv \"$f\" \"$(echo "$f" | sed -nE 's/(.{16})_(.*)(\.[a-z]*)$/\2_\1\3/p')\"; done > runme
Then review the "runme" file. If it looks good, run it (well, source it):
. ./runme
Of course, do not forget step zero: have backups.
Also note that your definition of "Project Name" does not specify whether it can include a period. I've assumed it can. If it can't, the sed command could be a bit simpler.
But if you want to stick with "rename", the regex only needs three captures - or two if you just run pdfs and docxs individually:
rename "s/(.{16})_(.*).pdf/\2_\1.pdf/"
rename "s/(.{16})_(.*).docx/\2_\1.docx/"
But this is also assuming "rename" uses extended regular expressions by default. (I'd also hope it has some "dryrun" mode where it will print what it will do before doing it. If so, use this option).
(edit: added missing -n to sed)
Last edited by Trilby (2024-03-17 22:39:25)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
thank you for the quick help Trilby, you`re a machine!!
i haven`t seen any periods in "Project Name" and the first command you provided works very well and is easier for me to apply than using rename. i have only one tiny issue, the file name gets duplicated
mv "companyxy-6do5zb_Project Name.jpg" "Project Name_companyxy-6do5zb.jpg Project Name_companyxy-6do5zb.jpg"
EDIT:
i haven`t tested the rename command yet since it`s already late here...
Last edited by espritlibre (2024-03-17 22:35:07)
Offline
Sorry about that - I missed the -n flag to sed, I've edited the previous post with the fix.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
thank you Trilby, this works like a charm! can i donate you a beer in XMR for that?
Offline