Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 02 Jan 1999 00:58:23 +1000
From:      Greg Black <gjb@acm.org>
To:        Don Read <sysop@calcasieu.com>
Cc:        Patrick Seal <patseal@hyperhost.net>, freebsd-questions@FreeBSD.ORG
Subject:   Re: lowercase filenames 
Message-ID:  <19990101145824.16515.qmail@alice.gba.oz.au>
In-Reply-To: <3.0.5.32.19981230164443.008ece70@mail>  of Wed, 30 Dec 1998 16:44:43 CST
References:  <3.0.5.32.19981230164443.008ece70@mail> 

next in thread | previous in thread | raw e-mail | index | archive | help
This is a general unix question (and therefore not relevant to
this list), but the following answer requires comment:

> >I have a directory with filenames like:
> >FOO.xxx
> >Bar.yyy
> >hex.zzz 
> >
> >What would be the easiest way to convert them to lowercase (and possibly
> >checking for existing lowercase counterparts before overwriting them)
> 
> #!/bin/sh
> 
> for old in *[A-Z]*
> do
>         new=`echo "$old" | tr "[A-Z]" "[a-z]"`
>         mv $old $old.tmp

What if $old.tmp exists?

>         if [ -f "$new" ]
>         then
>                 echo backup $new "->"  $new.bak
>                 mv $new $new.bak

What if $new.bak exists?

>         fi
>         echo $old "->" $new
>         mv $old.tmp $new
> done

The moral here is this:  if you're going to pretend to provide
checks to avoid overwriting existing files, make sure you do it
right because getting it wrong when you seem to be trying to be
safe is worse than clearly not trying at all.

A much better solution is simply:

#!/bin/sh
for old in *[A-Z]* ; do
  new=$(echo $old | tr A-Z a-z)
  [ -e $new ] && echo - $new already exists || ( set -x ; mv $old $new )
done

-- 
Greg Black <gjb@acm.org>


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19990101145824.16515.qmail>