Remove dashes from filenames

This removes spaces in filenames.

How can I also remove dashes - ?

rename "s/ //g" *

Solution 1:

rename in Ubuntu is a perl tool, it uses perl syntax:

s/regex/replacement/modifiers

In your case:

  • regex is a space (what you want to replace)
  • replacement is empty (you want to replace with nothing)
  • modifiers is g (don't stop after first regex match)

So you need to understand how to edit the regex to match dashes:

Simply use this to remove dashes:

rename 's/-//g' *

But if you want to remove both dashes and space, you may use character classes.

So this will do it for you:

rename 's/[- ]//g' *

Please note, when any of the filenames begins with a -, it won't work. Please see @BillPoser's answer.

Solution 2:

rename 's/-//g' *

doesn't work when any of the filenames begins with a dash, because it is then interpreted as initiating an option. You need to use the -- flag, which terminates the option string:

rename -- 's/-//g' *