Remove bad characters from filenames

June 24, 2015 Stanly G Linux, Files 0 minutes, 24 seconds

I've managed to have issues with filenames that are perfectly legitimate, but can't be copied onto Windows (or sometimes Mac clients), because of characters that aren't allowed per the file system specificiations.

The solution: Get rid of them with a one-line shell command.

for file in *; do mv "$file" $(echo "$file" | sed 's/[^A-Za-z0-9_-\.]/_/g'); done

Et voila! Only alphanumeric, underscores, hyphens, and periods. Those names work across Windows, Mac, and Linux, as long as the file name isn't too long.