[Gllug] Making filenames 'FAT-safe' in bash

- Tethys tethys at gmail.com
Tue Nov 30 13:01:50 UTC 2010


On Tue, Nov 30, 2010 at 12:30 PM, Dylan <dylan at dylan.me.uk> wrote:

> I have a bash script which takes a list of files and copies them to a usb
> stick, partially re-creating the original directory structure as it goes. This
> all functions as expected until it reaches a file with non-FAT compatible
> characters. So, how do I remove these characters? I need to handle filenames
> with ' and " in them as well as ?, !, (, ), [, ], & ...

I do it with tr and sed, for creating mp3 filenames that the car
stereo doesn't barf on:

safename=$(echo "$name" | tr '[:upper:]' '[:lower:]' | \
		sed 's/ *- */_/g;s/  */_/g' | \
		sed -f /local/share/deunicode.sed | \
		sed 's/[^0-9a-z_]//g;s/__*/_/g')

deunicode.sed contains:

	s/[ß]/ss/g
	s/[æ]/ae/g
	s/[Æ]/AE/g
	s/[äáàâå]/a/g
	s/[ÄÁÀÂÅ]/A/g
	s/[çćĉ]/c/g
	s/[ÇĆĈ]/C/g
	s/[ëéèêę]/e/g
	s/[ËÉÈÊĘ]/E/g
	s/[ïíìî]/i/g
	s/[ÏÍÌÎ]/I/g
	s/[ñ]/n/g
	s/[Ñ]/N/g
	s/[öóòôø]/o/g
	s/[ÖÓÒÔØ]/O/g
	s/[ŝ]/s/g
	s/[Ŝ]/s/g
	s/[üúùûŭ]/u/g
	s/[ÜÚÙÛŬ]/U/g
	s/[ÿý]/y/g
	s/[ŸÝ]/Y/g

It's not perfect, but it's good enough for what I need (and probably
for what you need).

Tet

-- 
"Any sufficiently advanced financial instrument is indistinguishable from fraud"
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug


More information about the GLLUG mailing list