[clug] renaming files to make windows happy

Nemo Thorx wombat at nemo.house.cx
Mon May 4 18:15:04 GMT 2009


On Thu, Apr 23, 2009 at 05:52:14PM +1000, Hal Ashburner did utter:
> 
> 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
> 
> 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.
> 
> 
> I'm not sure if it's exactly what you want but I hope it helps.
> 
> Hal Ashburner

Just to update and feed the collective wisdom...

This and other suggestions were a good start. The character set listed
was a little too limited for my needs though (I wanted the biggest
subset of characters handled by both linux commandline and win32 API
- and thus requiring minimum of changes.  

What I ended up with was a wrapper around this core:

$(echo -n "$file" |tr :\"\*?\! -\'#__ | sed -e 's/\.*$//g')

Where 'tr' translates a bunch of WIN32 unfriendly characters into win32
agreeable ones, using some guesses as to context rather than just
turning everything into an _. I then hit 'sed' up to remove any trailing
fullstops.

This isn't bulletproof since it's trivial to craft examples where this
still will leave a win32 unfriendly filename (I don't translate all
characters, and will leave files with trailing spaces), but for my needs
at the moment, this did me fine. 

cheers
.../Nemo

-- 
  ------------------------------------------ --------------------------
                                                    earth native


More information about the linux mailing list