[clug] renaming files to make windows happy

Daniel Pittman daniel at rimspace.net
Tue May 5 01:18:08 GMT 2009


Nemo Thorx <wombat at nemo.house.cx> writes:
> 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.
>
> 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.

FWIW, I often use the 'rename' script shipped with Perl for this:

  rename 'tr {:\"\*?\!}{-\'#__}; s/\.*$//g' *

rename applies the first argument as a Perl expression with $_ bound to
the filename, then safely renames the first to the second.

It takes some of the wrapping and pipelining out of the easy renames,
and gives scope to do more complex things more easily, I find.

Regards,
        Daniel


More information about the linux mailing list