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.





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 \ }


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/^.*(S\d+E\d+).*\.(.*?)$/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.

Answered By – Hasturkun

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published