[clug] renaming files to make windows happy

Hal Ashburner hal.ashburner at gmail.com
Thu Apr 23 08:22:47 GMT 2009


Kim Holburn wrote:
>
> 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
This is definitely an improvement except for one thing.
I'd leave the echo in before mv "$file" "$new" and still check the test 
run very carefully before firing it off.

Other tricks are, if you've got bags of spare disk space, create a 
parallel top level directory and use cp -a rather mv in the above script
use mv -i so you're prompted about overwriting
If your filesystem handles it, (don't know about ntfs on this score) 
create hardlinks (using ln) with the new names, check those, then delete 
the originals if you decide they're all good.

Oh and that tr? I made at least one small error I can see, the - should 
be outside the []
tr -c '[a-zA-Z0-9_.]-' _

Anyone got any other tricks for checking large scripted operations where 
discovering the mistake late, would be pretty painful?





More information about the linux mailing list