subst is a helpfull tool I use almost every day. You can rename files with it or substitute file contents using perl regular expressions.

Today I published a new version (1.1.4) which goes one step further and allows to rename directories as well. And it is possible to do this recursively. Really nice.

As an comprehensive example, today I issued this command to rename all files and directories in a mp3 folder I received by a friend. These files and directories had all sorts of nonsense characters in them (which is everything I don’t need in a commandline environment):

subst -R -m 's/\W/-/g' -m 's/_/-/g' -m 's/-+/-/g' -m 's/-mp3/.mp3/' -m 's/([A-Z])/lc($1)/ge' -m 's/-$//' mp3dir

Ok, looks weird, let’s dive into it:

  • -R recurse into mp3dir

  • -m 's/\W/-/g' Replace all non-word characters with a dash.

  • -m 's/_/-/g' Replace all underscores with dashes.

  • -m 's/-+/-/g' Replace all excess dashes with one (like --- to -)

  • -m 's/-mp3/.mp3/' Turn the suffix into a suffix again, since \W (see above) also captures the dot.

  • -m 's/([A-Z])/lc($1)/ge' Lowercase all upper case letters (easier to type).

  • -m 's/-$//' Finally remove trailing dashes, which occur from time to time (e.g. if a filename ends with .).

Find the latest version here. Copy to your disk, rename to subst and use it. Documentation is included.