13

I’m using a GNU/Linux distribution where the utility rename comes from util-linux and I want to make full use of regular (Perl or POSIX) expressions with it.

There are two versions of rename :

  • The “Perl” version, with syntax rename 's/^fgh/jkl/' fgh*
  • The util-linux version, with syntax rename fgh jkl fgh*

If the use of regexes seems pretty obvious with the first one, to which I have no easy access. However, I’m confused about the second one: I could not find any relevant documentation or examples on the possible use, and in that case the format, of the regular expressions to use.

Let’s take, to make a simple example, a directory containing:

foo_a1.ext
foo_a32.ext
foo_c18.ext
foo_h12.ext

I want to use a syntax like one of these two lines:

rename "foo_[a-z]([0-9]{1,2}).ext" "foo_\1.ext" *
rename "foo_[:alpha:]([:digit:]{1,2}).ext" "foo_\1.ext" *

for which the expected output would be:

foo_1.ext
foo_32.ext
foo_18.ext
foo_12.ext

Of course this does not work! Either I’m missing something obvious, or there is no implemented way to use actual regular expressions with this tool.

(Please note that I am aware of the other possibilities for renaming files with regular expressions in a shell interpreter; this question aims at a specific version of the rename tool.)

Community
  • 1
  • 1
Arcturus B
  • 5,181
  • 4
  • 31
  • 51
  • 1
    It seems there is no regex support for `util-linux` so are you looking for a solution with the `perl` version even if you have "no easy access to it"? – Alepac Jul 14 '15 at 14:18
  • @Alepac: given the extensive use I could make of such a tool, I’m considering going through the trouble of installing the “Perl” version. Figuring out how it works should not be too hard once installed! – Arcturus B Jul 14 '15 at 14:21
  • Can't you use a bash loop with bash builtin regexp to do that? – Alepac Jul 14 '15 at 15:07
  • @Alepac: the whole point of this is avoiding the use of a bash loops. The “Perl” version worked perfectly fine once installed! Thanks! – Arcturus B Jul 14 '15 at 15:16

2 Answers2

9

Here is the manual page: http://linux.die.net/man/1/rename. It is pretty straightforward:

rename from to file...

rename will rename the specified files by replacing the first occurrence of from in their name by to.

I believe there are no regexes, it is just plain substring match.

buff
  • 2,063
  • 10
  • 16
  • Thanks for your answer! Seeing you were not mentioning regular expressions, I edited my question to add an example… and in the mean time you added the last precision! I suspect the lack of regex support as well but I’d like someone to give some stronger evidence! – Arcturus B Jul 14 '15 at 14:13
  • 1
    The man page uses the term `expression` to identify the string to match. Yet nowhere is their any indication that this is an expression other than just a string literal. It seems like a case of unfortunate naming. – NeilG Jan 13 '22 at 02:27
-5

The following command gives the expected result with your input file but using the perl version:

rename 's/foo_\D+(\d+)/foo_$1/' *.ext

You can test the command using -n option to rename

Alepac
  • 1,833
  • 13
  • 24