Mass re-name files 

http://groups.google.com/groups?selm=asbq9v%24pfsil%241%40ID-136730.news.dfncis.de

Newsgroups: comp.os.linux.help, comp.os.linux.misc
Date: 2002-11-30 17:57:22 PST
> > 6.)  Mass re-name files:  The best for last :).  I'm not a programmer, but
> > I've been trying to figure out a string that will rename all the files &
> > directories in a directory.  Right now, files & directories are a mix of
> > cases - I'd like to have everything be lower-case. For example:
>
> $ for  f  in *  ; do  mv $f  $( echo  $f  |  tr   A-Z   a-z )
>   ; done
>
> That's all one line.
  1. Which will fail if any file names contain spaces (or other problem characters).
  2. Which will generate error messages if the name is already lower case (as in one of the examples).
  3. Which will cause problems if another file already exists with the name in lower case.

The first problem is easy to fix; just quote the file names:

mv "$f" "$( echo $f | tr A-Z a-z )"

The second isn't hard, but it's more than you would want in a single line:

newname=$( echo $f | tr A-Z a-z )
[ "$f" != "$newname" ] && mv "$f" "$( echo $f | tr A-Z a-z )"

The third requires a decision on what to do if a file with the new name already exists. Do you want to overwrite it? Rename it? Leave it as is?

[ "$f" != "$newname" ] &&
  if [ -e "$newname" ]
  then
     ## what goes here depends on the decision mentioned above.
  else
     mv "$f" "$( echo $f | tr A-Z a-z )"
  fi

Chris F.A. Johnson