Remove hyphens from filename with Bash

bash, rename, shell

Solution

Try this:

for file in $(find dirWithDashedFiles -type f -iname '*-*'); do
  mv $file ${file//-/}
done

That's assuming that your directories don't have dashes in the name. That would break this.

The `${varname//regex/replacementText}` syntax is explained here. Just search for substring replacement.

Also, this would break if your directories or filenames have spaces in them. If you have spaces in your filenames, you should use this:

for file in *-*; do
  mv $file "${file//-/}"
done

This has the disadvantage of having to be run in every directory that contains files you want to change, but, like I said, it's a little more robust.

Problem

I am trying to create a small Bash script to remove hyphens from a filename. For example, I want to rename: CropDamageVO-041412.mpg to CropDamageVO041412.mpg I'm new to Bash, so be gentle :] Thank you for any help

Original source