“Fixing” troublesome filenames.

Windows has a number of reserved characters that aren’t allowed in file/folder names:

◦The following reserved characters:
◾< (less than) ◾> (greater than)
◾: (colon)
◾” (double quote)
◾/ (forward slash)
◾\ (backslash)
◾| (vertical bar or pipe)
◾? (question mark)
◾* (asterisk)

I’d also advise against the use of curly braces {}

The following code excerpt can be used to “clean up” a folder full of files and get rid of the major show-stoppers. In this excerpt I replace certain characters like space and : with _ and delete other characters. I also replace an resulting “double spaces” __ with a single _

Feel free to expand and adapt as necessary.

Piping ls into a while read gets around the need to try and properly escape everything when sending it to mv

###
# Fix badly names files
# Windows doesn't really like "::" in filenames, we'll also get rid of other problematic characters.
###

ls | while read -r FILE ; do
  mv -u "${FILE}" $(echo "${FILE}" | tr ' ' '_' | tr -d '[{}(),\!]' | tr ':' '_' | sed 's/__/_/g') 2>/dev/null
done

Comments are closed, but trackbacks and pingbacks are open.