0

I've been going round in circles!

I specifically need to remove the fifth character from filenames using 'rename' in Kubuntu linux.

That character appears elsewhere in the names so searching for the char will fail.

I've seen the following:

rename 's/-.*\././' -- *.mp3 *.mp3 

But examination of the rename help file does not has s or its function described

Thanks

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • see: https://unix.stackexchange.com/a/510583/170373 for some notes on the different rename commands – ilkkachu Oct 10 '21 at 16:49

1 Answers1

4

There are two versions of rename in common use. If you've the perl one, sometimes also known as prename, the syntax uses a Regular Expression embedded into a perl operation such as "substitute". To remove the fifth character you need to match on the first four, and then any remaining from character six onwards.

Here's an appropriate RE. The first bracketed expression matches an optional path component. The second matches the beginning of the filename. We don't need to match the end of the filename as well not be changing anything in it, and it's not needed as an anchor.

^(.*/)?(....).

A dot corresponds to "any character". The uparrow binds to the start-of-string. A .* sequence means "zero or more characters". A question mark allows the preceding segment to be optional. The bracketed sections can be reapplied in the matching part as $1, $2, etc.

Putting this into the "substitute" command (s) for all files matching the glob *.mp3 you get this:

rename 's/^(.*/)?(....)./$1$2/' ./*.mp3
Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • Thanks Roaima, that is very informative but is complicated for me. I have 100 file names that are all different except the all have a redundant underscore as the last character of their name, it is that underscore I wish to remove. – Christopher Bruce Oct 11 '21 at 13:18
  • @ChristopherBruce I've given you the command at the end of the answer – Chris Davies Oct 11 '21 at 13:48