Change spaces or dots in file name to dashes

Toby Kelsey toby.kelsey at
Fri May 11 12:41:22 UTC 2007

OOzy Pal wrote:
> How can change spaces or dots in a file name to dashes using the Console?

   rename -v 's/[\s.]/-/g' foo*

where foo* matches your particular filename(s), or

   rename -v 's/[^\w\d\-]/_/g' *

to replace all non-alphanumeric-or-dash characters with underscores in all files
in the current folder.  Sometimes filenames can contain tabs or control
characters which are difficult to handle.  You could make this an alias:

alias cleannames="rename -v 's/[^\w\d\-]/_/g' *"

and put it in your .bashrc

'rename' is part of the 'perl' package, which you need installed.  To understand
more about regular expressions (the 's/foo/bar/' bit), install 'perl-doc' and do

   man perlrequick


More information about the ubuntu-users mailing list