Ubuntu – Remove parenthesis and brackets from multiple directories

batch-renamecommand line

I'm trying to find a way to remove parenthesis and brackets from directories, but they don't have all the same pattern.
Some are like this: (1234) ABC [xyz]. Others are like this [xyz] ABC (1234)(987).

Edit: The desired output would be ABC

How can I do this?

Best Answer

You can use rename to remove any of those characters wherever they occur with alternation

rename -n 's/\(|\[|\]|\)//g' *

You need to escape the brackets and parentheses.

Remove -n after testing to really do the renaming.

To remove all the characters within brackets or parentheses

rename -n 's/\(.*\)|\[.*\]//g' *

To also remove spaces (to change (1234) ABC [xyz] to ABC)

rename -n 's/\(.*\)|\[.*\]| //g' *