[clug] renaming files to make windows happy

Hal Ashburner hal.ashburner at gmail.com
Thu Apr 23 07:52:14 GMT 2009

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

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 ] 

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

Hal Ashburner

More information about the linux mailing list