[wellylug] Getting rid of illegal characters in filenames
jumbophut
jumbophut at gmail.com
Tue Nov 23 15:03:50 NZDT 2004
On Mon, 22 Nov 2004 19:25:44 +1300, Jamie Dobbs wrote:
> A while ago (ok maybe a long time ago!) someone gave me a piece of code
> that would go through the files in a directory (actually having it
> traverse an entire directory tree would be better) and rename any files
> that contained "illegal" characters (ie. ones Windows/DOS doesn't like).
> I have since misplaced the code and would appreciate any help that
> people can give me to fix these filenames that contain illegal
> characters (such as * $ % etc).
>
The script is probably straightforward if you are willing to take some
risk. I have no Linux at work, but something like this should work:
for i in $(find /path/with/files | grep -e '[*$%]'); do # list all
files, find illegal ones.
mv $i $(echo $i | tr '*$%' 'xyz'); # for each file, mv, replacing
illegal chars with legal
done;
HOWEVER, there is, as I mentioned, a risk. Any change you make risks
overwriting an existing file. e.g. you have my%file. If you change
it to myzfile, it might overwrite an already existing myzfile.
I'm not sure of an easy way to solve this, short of checking for the
existence of a pre-existing file and changing the replacement char
(potentially repeatedly if you keep getting matches with existing
files).
--
Tony (echo 'spend!,pocket awide' | sed 'y/acdeikospntw!, /l at omcgtjuba.phi/')
More information about the wellylug
mailing list