[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Converting filenames to lowercase in a directory



On Sun, Jun 23, 2002 at 11:39:43PM -0500, SILUG wrote:
> Is there an easy way to convert all of the filenames in a given
> directory to lowercase?

    for file in *[A-Z]* ; do
        mv -i "$file" "`echo $file | tr A-Z a-z`"
    done

Or there's the "right way" to do that with tr...

    for file in *[A-Z]* ; do
        mv -i "$file" "`echo $file | tr '[[:upper:]]' '[[:lower:]]'`"
    done

Looking at that, I just realized that it will also have the
interesting side-effect of getting rid of all but one space (by
itself) in any file name.  In other words, a file named "a    b   c"
would be renamed "a b c".  That's because the $file after echo isn't
quoted.  Hmm...

(Pause while I try something.)

I'm not 100% sure how portable this is, but bash2 seems to be smart
enough to let you nest quotes.  How cool is that?

    for file in *[A-Z]* ; do
        mv -i "$file" "`echo "$file" | tr '[[:upper:]]' '[[:lower:]]'`"
    done

And then, of course, there's the way to do it using perl...

    perl -e 'for (@ARGV) { rename($_, lc($_)) if (/A-Z/) }' *

(Although that could use a little error checking, and it doesn't warn
you before clobbering files.)

> For simplicity, assume that doing the conversion would NOT cause any
> duplicate filenames.

The "-i" on mv will at least prompt you before clobbering any files.

If you are sure there are no duplicate filenames, and there are no
filenames with whitespace, you can reduce that down to this:

    for file in *[A-Z]* ; do mv $file `echo $file | tr A-Z a-z` ; done

Steve
-- 
steve@silug.org           | Southern Illinois Linux Users Group
(618)398-7360             | See web site for meeting details.
Steven Pritchard          | http://www.silug.org/

-
To unsubscribe, send email to majordomo@silug.org with
"unsubscribe silug-discuss" in the body.