Title: bad-filenames-change.sh.2015.05.27
Created: 2018-04-29 22:24:09

A shell script to take bad characters out of filenames


#!/bin/bash


find . -depth -name "*[^a-zA-Z0-9._-]*" |     # Find all files or folders containing 'bad' characters.
while read FILEDIR                            # Read them line-by-line.
do
        DIR="${FILEDIR%/*}"                   # Get the folder its inside
        FILE="${FILEDIR/*\/}"                 # Get the plain name.
        NEWFILE="${FILE//[^a-zA-Z0-9._-]/_}" # Substitute _ for bad things.
        mv -nv "$DIR/$FILE" "$DIR/$NEWFILE"  # Rename it but do not overwrite existing files
done