I want to rename many files like

Tum Hi Ho [www.DJMaza.Com].mp3 to Tum Hi Ho.mp3

To do so, I used this command. But somehow it is not working.

`rename -n 's/(.*)([.*])(\.mp3)/$1$3/' *.mp3`
share|improve this question

migrated from serverfault.com Oct 1 '16 at 6:42

This question came from our site for system and network administrators.

You were close, but not quite:

rename -n 's/\[[^\]]+\]//' *.mp3

The problem is that [] is a special regex construct and therefore the brackets need to be escaped if they are to match actual brackets. That's the purpose of the backslashes in my version.

The unescaped brackets form what is called a character class. A character class can basically match any of the characters inside the brackets. So [.*] matches a period or an asterisk. When you add a caret (^) to the beginning of a class, it matches any character not within the brackets.

In my regex, we are matching an opening bracket \[ plus one or more characters that are not closing brackets [^\]]+ plus a closing bracket \] and removing all that.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.