You are not logged in.

#1 2024-03-17 20:53:50

espritlibre
Member
Registered: 2022-12-15
Posts: 132

copy first 16 characters of a filename to the end of filename

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

#2 2024-03-17 21:34:01

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: copy first 16 characters of a filename to the end of filename

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

#3 2024-03-17 22:33:45

espritlibre
Member
Registered: 2022-12-15
Posts: 132

Re: copy first 16 characters of a filename to the end of filename

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

#4 2024-03-17 22:39:57

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: copy first 16 characters of a filename to the end of filename

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

#5 2024-03-19 19:36:16

espritlibre
Member
Registered: 2022-12-15
Posts: 132

Re: copy first 16 characters of a filename to the end of filename

thank you Trilby, this works like a charm! can i donate you a beer in XMR for that?

Offline

Board footer

Powered by FluxBB