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`

1 Answer 1


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.

You must log in to answer this question.