From owner-freebsd-questions Fri Jan 1 07:04:07 1999 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id HAA20902 for freebsd-questions-outgoing; Fri, 1 Jan 1999 07:04:07 -0800 (PST) (envelope-from owner-freebsd-questions@FreeBSD.ORG) Received: from alice.gba.oz.au (gba-254.tmx.com.au [203.9.155.254]) by hub.freebsd.org (8.8.8/8.8.8) with SMTP id HAA20875 for ; Fri, 1 Jan 1999 07:04:00 -0800 (PST) (envelope-from gjb@acm.org) Received: (qmail 16516 invoked by uid 1001); 1 Jan 1999 14:58:24 -0000 Message-ID: <19990101145824.16515.qmail@alice.gba.oz.au> X-Posted-By: GBA-Post 1.03 20-Sep-1998 X-PGP-Fingerprint: 5A91 6942 8CEA 9DAB B95B C249 1CE1 493B 2B5A CE30 Date: Sat, 02 Jan 1999 00:58:23 +1000 From: Greg Black To: Don Read Cc: Patrick Seal , freebsd-questions@FreeBSD.ORG Subject: Re: lowercase filenames References: <3.0.5.32.19981230164443.008ece70@mail> In-reply-to: <3.0.5.32.19981230164443.008ece70@mail> of Wed, 30 Dec 1998 16:44:43 CST Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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 To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message