[clug] renaming files to make windows happy

Francis James Whittle fudje at grapevine.net.au
Thu Apr 23 08:20:58 GMT 2009

On Thu, 2009-04-23 at 17:52 +1000, Hal Ashburner wrote:
> The /portable filename characters/ as defined by ANSI C are
>  a b c d e f g h i j k l m n o p q r t u v w x y z
>  A B C D E F G H I J K L M N O P Q R T U V W X Y Z
>  0 1 2 3 4 5 6 7 8 9
>  . _ -
> prompt$ for file in *; do echo mv "$file" $(echo -n "$file" |tr -c '[a-zA-Z0-9_.-]' _ );done

This doesn't seem to do question marks (?).  I had a similar suggestion:
for file in *; do echo mv "$file" "$(sed -e 's/[^a-zA-Z0-9_.-]/_/g' <<<
\"$file\")"; done

<<< is shell redirection from a string :)

Curiously recent version of sed seem to skip some latin-1 / unicode
characters with this one (eg. á ñ ç and the like).  My memory is that
win32 handles these just fine, but I could be wrong.

> Is what I'd use, the output of which I'd check very carefully for "bulletproofness" before using.
> If you've got directories with dodgy names in a hierarchy then 
> find . -name "*"; while read dir; do
>    if [ -d $dir ] 
> etc.

you can make find only return directories with the -type d test:

find . -type d; while read dir; do ...; done

Saves use of an extra conditional.

> I'm not sure if it's exactly what you want but I hope it helps.
> Hal Ashburner

Back to Java homework....

More information about the linux mailing list