On Wed, Jan 02, 2002 at 03:26:09PM -0800, gabriel wrote:
> i can't be the only one to have asked this
> but how do you convert the case of a whole load of files
> 
> ie: convert FiLE.Txt to file.txt
> 
> without doing the following:
> rename A a *
> rename B b *
> rename C c *
> ...
> 
> there has to be an easier way...

There sure is!

The shell (e.g., bash) is not merely a command interpreter (as it is in
MessyDOS), but it is a flexible and powerful programming language.

Lets say that you want to convert all filenames in a given directory
from upper or mixed case to all lowercase. you could do it easily, like
this:

        cd targetdir
        for i in (*)
                do
                mv $i `echo $i | tr "[A-Z]" "[a-z]"`
                done

The line:
        for i in (*)
selects all items (including subdirectory names) in the current directory.
The:
        do
        ...
        done
marks the beginning and end of the block of code that is repeated by
the for loop.

The:
        mv $i `echo $i | tr "[A-Z]" "[a-z]"`
does the actual dirty work. The variable "i" contains the name of one 
item from the directory. Each time around the loop it gets the name
of the next item. So, we're renaming the file to something else.
But what else? The new name will be the result of the command:
        echo $i | tr "[A-Z]" "[a-z]"
if $i contains "NoWisThe.Time", the result of this command will be 
"nowisthe.time". note that we're hiding that sequence inside (what
is commonly called) 'backquotes' or 'backticks':
        `stuff`
NOT
        'stuff'
The two are quite different. Doing:
        `echo $i | tr "[A-Z]" "[a-z]"`
evaluates to the TEXT result of the echo|tr command, so that the 
final command turns into:
        mv File.One file.one
        mv File.tWo file.two
        mv FILe.ThrEE file.three
        etc.

Also, check out the man page on 'tr' to see all the cool things you can
do with it.

Fred
-- 
---- Fred Smith -- [EMAIL PROTECTED] -----------------------------
                       I can do all things through Christ 
                              who strengthens me.
------------------------------ Philippians 4:13 -------------------------------



_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to