Return to Snippet

Revision: 7503
at July 27, 2008 17:34 by jonhenshaw

Initial Code
# lowerit
# convert all file names in the current directory to lower case
# only operates on plain files--does not change the name of directories
# will ask for verification before overwriting an existing file
for x in `ls`
    if [ ! -f $x ]; then
    lc=`echo $x  | tr '[A-Z]' '[a-z]'`
    if [ $lc != $x ]; then
        mv -i $x $lc

Initial URL

Initial Description
Line 6 starts a loop (which ends with line 15). The ls command returns a list of filenames which are sequentially assigned to the shell variable x. The if test (lines 8 through 10) checks to see if the current filename is that of a plain file. If not, the remainder of the statements in the current loop iteration are skipped.

If line 11 is to be executed we know that we have an ordinary file. Using tr we convert the filename to lowercase and assign the new name to the shell variable lc. Line 12 then checks to see if the lowercase version of the name differs from the original. If it does, line 13 is executed to change the name of the original file to the new lowercase name. The -i option causes the mv to prompt for confirmation if executing the command would overwrite an existing filename. __________________________

Initial Title
Howto Convert Filenames to All Lowercase

Initial Tags

Initial Language