> I'm very green with Linux still and I'm trying to overcome a huge nuisance.
> Many files I receive are from Windows users who *love* to embed funky
> characters in their file names. Is there is clever way to reformat the
> filenames in batch mode? I've tried writing a simple script using 'ls', 'xargs'
> to build my set of filenames for processing, but the problem I'm seeing
> is a filename like:
> This is a windows filename.gif
> appears like this inside of a scripting for loop (who's echoing the
> filename):
> This
> is
> a
> windows
> filename.gif
As I see it, you have two problems:
1. Funky file names (read as if s/n/c/)
The solution is, of course, to remedy problem 1, then problem 2
disappears.
However, a script to remedy problem 1 runs into problem 2.
Problem 2 is not hard to overcome, but I'd recommend doing it only once,
and changing the offensi^H^Hding filenames so they don't bother you
again.
To harvest the filenames, use find. For example, to find filenames
containing a space (I know it's legal to have spaces in file names;
drinking 3 bottles of scotch every day is legal, but it, too, is not
advisable):
find "$DIR" -name '* *' [-print] ## -print is not necessary with most
## modern versions of find
To convert the filename to a saner moniker:
newname=`echo "$FILENAME" | tr -cd '[a-zA-Z0-9.\-]'`
The characters between the square brackets are those acceptable (sane,
not just legal) for file names. They are all upper- and lower-case
letters, the digits 0 to 9, and '-' and '.'. If you like, you could
include some other characters such as '~'.
Put it together and you have:
DIR=dir_with_a_problem
find "$DIR" -name '* *' | ## adjust to catch other weird characters
while read filename
do
newname=`echo "$filename" | tr -cd '[a-zA-Z0-9.\-]'`
mv "$filename" "$newname"
done
You should check for collisions between sanitized filenames, but this
gives you the basic method. Other denizens of this group will help you
plug the holes.
--
Chris F.A. Johnson http://cfaj.freeshell.org
===================================================================
My code (if any) in this post is copyright 2001, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License