Skip to content
Advertisement

Linux rename s/ – regex for wildcard single characte r

I have found a simple solution to my actual requirement, but I would still like to understand how to use the regex equivalent of the single character wildcard ? which we use for filtering … in say ls

I would like to rename a group of files which differ by one character.

FROM

Impossible-S01E01-x264.mkv
Impossible-S01E02-x264.mkv
Impossible-S01E03-x264.mkv
Impossible-S01E04-x264.mkv
Impossible-S01E05-x264.mkv

TO

Impossible-S01E01.mkv
Impossible-S01E02.mkv
Impossible-S01E03.mkv
Impossible-S01E04.mkv
Impossible-S01E05.mkv

As I said above, my simple solution is:

rename s/-x264// *.mkv

That sorts out my needs – all good and well – but I really want to understand my first approach:

To list the files, I can use:

ls Impossible-S01E0?-x264.mkv

So what I was trying for the rename was:

rename s/Impossible-S01E0?-x264.mkv/Impossible-S01E0?.mkv/ *.mkv

I have read up here:

How do regular expressions differ from wildcards used to filter files

And here:

Why does my regular expression work in X but not in Y?

I see this:

. matches any character (or any character except a newline).

I just can’t seem to wrap my head around how to use that – hoping someone will explain for my education.

{ edit: missed a backslash }

Advertisement

Answer

So, regular expressions aren’t globs. If you wanted to keep the middle (e.g. catch the season/ep) and replace everything else, you’d need to use capture groups. e.g. s/^.*(Sd+Ed+).*.(.*?)$/Foo-$1.$2/

This would extract an SxxExx and the file extension, throw everything else away, and compose a new filename.

In a bit more detail it:

  1. Matches everything from the start until an SxxExx (where xx is actually any number of digits)
  2. Captures the contents of SxxExx
  3. Matches everything until the final literal .
  4. Non-greedily matches everything after the ., which it captures.

For your specific case of removing a suffix, this is likely overkill, though.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement