3

I'm trying to rename files in a folder which has files with a name like:

['A_LOT_OF_TEXT'].pdf&blobcol=urldata&blobtable=MungoBlobs

  • I basically want to obtain everything before the '.pdf' including the '.pdf'

After install brew rename, I tried using this but it does not work:

rename -n -v  '.*?(.pdf)' *

I get the error:

Using expression: sub { use feature ':5.28'; .*?(.pdf) }
syntax error at (eval 2) line 1, near "; ."

Any solution to this?

Clancento
  • 47
  • 4

2 Answers2

3

You certainly want

rename -n -v  's/^(.*?\.pdf).*/$1/' *

See regex proof. Remove -n once you are sure the expression works for you well.

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to $1:
--------------------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    pdf                      'pdf'
--------------------------------------------------------------------------------
  )                        end of $1
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37
  • you can combine your flags, for example you can just say `rename -nv 's/^(.*?\.pdf).*/$1/' *` to test and then rename `rename -v 's/^(.*?\.pdf).*/$1/' *` to execute – spectrum Oct 21 '22 at 17:17
2

I would advise you to look at the example usage in the manpage for rename.
One possible solution is as follows:

rename -v -n 's/(.*pdf).*/$1/' *

Explanation:

-v: print names of files successfully renamed
-n: dry run; don't actually rename
$1: First group in the matched regex
*: Run on all files in directory

Read more about the substitute command here.

trunc8
  • 21
  • 5