> Is there a standard Linux command to change all files in a directory and in 
> all its subdirectories to lower case?

No, not a single command, but this is the type of thing that can be
relatively easily done with a shell script.

First, as you probably know, 'mv' renames as well as moves (there is no
separate "rename" command in Unix, since a 'mv' suffices to "move" the
file to a new name.)

Second, if you take a string and/or file and use the 'tr' command on it,
you can (among other things) change the case from upper to lower case, or
vice versa. You can also use 'sed' for this purpose.

In order to do what you suggest, you will need to feed the file names
one at a time through 'tr' or 'sed' and them use 'mv' to rename the upper
case filename to the equivalent lower cased filename. And, you'll need to
automate this through a whole directory and any other directories under-
neath it. Seems like a tall order, but it's only three or so separate
steps.

(Incidentally, when tackling something like this for the first time, it's
best to try the commands individually -- for instance, use 'echo' and other
methods to simulate the task, just to see what will happen, and try the 
innermost command first rather than trying to do the whole directory at
once.)

So, let's try an example:

# i=FIRST
# echo $i | tr [A-Z] [a-z]

What does that print? It should print 'first'. This is the crux of the
whole operation. What did we do here? Well, we assigned a variable the
contents "FIRST" and then we echoed the contents of that variable, piping
it to a 'tr' process that handles the lowercasing.

Now, to make it a bit more complicated, we can take that previous expression
and substitute it for a second filename in a 'mv' command. To pull off this
trick, we need to put the command in backquotes `   ` (the character above
the tab key on most US keyboards). What the shell does is literally takes the
output of the command and *substitutes* the output right in place on the
command line. 

Try this:

# touch FIRST
# i=FIRST
# mv $i | `echo $i | tr [A-Z] [a-z]`

Try that in a directory. Do an 'ls'. What do you see? You should see a
filename named 'first'. Line #3 is the workhorse of the whole thing; it
takes a filename, sends it to a tr process, then renames the uppercase with
the output of the command in backquotes -- the result the shell sees
is 'mv FIRST first'.

Now, all we need to do is to automate this through a script that finds all
files under a given subdirectory. 'find' is the command we can use for this,
and a 'for' loop suffices, since it will "visit" each filename in turn:

# for i in `find -type -f` (we don't want to lowercase directories)
# do
# mv $i | `echo $i | tr [A-Z] [a-z]`
# done

Pretty simple, once you understand the background. And you can do quite 
a bit of powerful stuff with little constructs like this, called "shell
scripts".


> Michel Clasquin, D Litt et Phil (Unisa)
> [EMAIL PROTECTED]/unisa.ac.za
> http://www.geocities.com/clasqm
------------------------------------------------------------------------
David E. Fox                              Thanks for letting me
[EMAIL PROTECTED]                            change magnetic patterns
[EMAIL PROTECTED]               on your hard disk.
-----------------------------------------------------------------------

Reply via email to