regex to match troublesome characters in filenames

Just something I’ve found useful that I keep having to look up…

[^a-zA-Z0-9\.\-\_]*

(match all characters except a to z, A to Z, 0 to 9, dot/period, dash, and underscore… most modern OSs should handle these characters)

(use this in thunar, renamer, prename, etc. dealing with large batches of files… e.g. email backups where filenames are auto-generated with screwy characters).

An example of how this might be used…

sudo yum -y install prename
cd some-directory
prename 's/[^a-zA-Z0-9\.\-\_]/\_/g' -- *

Leave a Comment