[clug] renaming files to make windows happy

Kim Holburn kim at holburn.net
Thu Apr 23 08:07:46 GMT 2009


On 2009/Apr/23, at 9:52 AM, Hal Ashburner wrote:

> Nemo wrote:
>> Hi all
>>
>> I have a large quantity of files with various win32 unfriendly
>> characters in the filename. (primarily ", ? and :, though those
>> obviously aren't the only ones that win32 can't handle.
>> The files are going onto a NTFS partition (which as a fs can handle  
>> the
>> characters in filenames fine, and ntfs-3g puts them there for the  
>> sake
>> of "maximum portability and interoperability reasons" (http://www.ntfs-3g.org/support.html 
>> ). (imho, this argument is bunk
>> since the main reason I think someone would want to write to ntfs  
>> is for
>> interoperability with a WindowsOS.
>> Windows win32 layer can't handle all the characters.
>> NTFS-3G doco suggests exporting the filesystem through samba for
>> compatibility. I don't particularly want to route all the files  
>> through
>> an additional network layer since they're already on the right  
>> machine.
>> So:
>>
>> Does anyone have or know of a nice simple bulletproof script which  
>> will
>> take a filename, and make it safe to windows. (ie, convert win32
>> unfriendly characters to _ or ^ or similar? Handling filename  
>> escapes in shell
>> scripts sanely gives me willies ;)
>>
>> thanks in advance!
>> .../Nemo
>>
>
> 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


prompt$ for file in *; do
   new=$(echo -n "$file" |tr -c '[a-zA-Z0-9_.-]' _ )
   if [ ! -e "$new" ]; then
     mv "$file" "$new"
   else
     echo "error file \"$new\" exists"
   fi
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
>
> -- 
> linux mailing list
> linux at lists.samba.org
> https://lists.samba.org/mailman/listinfo/linux

-- 
Kim Holburn
IT Network & Security Consultant
Ph: +39 06 855 4294  M: +39 3494957443
mailto:kim at holburn.net  aim://kimholburn
skype://kholburn - PGP Public Key on request








More information about the linux mailing list