> I'm having some problems renaming some files created under windows. The > filenames contain characters that bash thinks are tokens... and causes the > usual "unexpected token complaint" > > An example file name would be: THIS is a (DUMB) windows filename & test > > A simple rename with mv is not possible, nor is a cp to another filename... > > What's the secret? I've been through a BIG search on google about this and it > appears > to be quite a well known problem... but I haven't found the answer. ;'(
Boy, do we care. Below is a script I use to convert those messy windows names of mp3's I download. It moves them into a file format that linux programs like. I an not even sure if this is needed, if you enclose all the names in quotes, but it works fine except for foreign letters (German titles don't do well, for example.) It just searches for all mp3's in the directory in which it is invoked, changes the MP3 to mp3, and then looks for \'s in the name, and fixes up dashes, spaces, (, ),and &'s. It makes a script called junkscript which you can inspect before you move your files to their new names. ==========Start of script============= # The trick is to remove all interfering characters. This include: # ' <space> # the subtle trick is to get the name of the imported mp3 so that the mv # command used in # a script can use it. This involved NOT surrounding the mp3 name with quotes # but # putting a \ in front of each Who'd have thunk it? dir -1 | grep \.[Mm][pP]3$ | sed "s/[mM][pP]3$/mp3/" | egrep "\\\\|'| |-" | tr "-" _ | \ tr -d \\\\ | tr " " _ | sed 's/\&/and/g' | tr -s _ | tr -d \' | tr -d \( | tr -d \) \ | nl > junk dir -1 | grep \.[Mm][pP]3$ | egrep "\\\\|'| |-" | sed 's/(/\\(/' | sed 's/)/\\)/' | \ sed 's/.*/mv &/' | sed "s/'/\\\'/g" | sed 's/\&/\\&/g' | nl > junk1 join junk1 junk | sed 's/^.* mv/mv/' > junkscript echo The script is saved in junkscript If you want a blow by blow description, here it is: Line 1: 1. dir -1 the directory and find all mp3's, whether caps or lower case 2. change MP3 to mp3 2. work only on files with the following characters: \ ' (space) - (That's the egrep statement) 3. change - to _ 4. delete all \'s 5. change space to _ 6. change & to and 7 squeeze out consecutive _'s. 8 delete all ' 9 delete all ( 10. delete all ) 11. Number the lines and put them in junk. That's all in the first line! The second line I believe just gets the files with funny windows names and inserts appropriate \'s in front of the funny characters. And it puts the mv statement in front of the windows name. It saves all this in junk1. The third line joins the two files into the junkscript file, which you should inspect to make sure it has what you want before you run it. Joel _______________________________________________ http://linux.nf -- [EMAIL PROTECTED] Archives, Subscribe, Unsubscribe, Digest, Etc ->http://linux.nf/mailman/listinfo/linux-users