[LBo] I'm trying to like it ...
Chris F.A. Johnson
cfajohnson at teksavvy.com
Wed Jan 24 17:23:24 CET 2007
On Wed, 24 Jan 2007, Douglas Orchard wrote:
>> ...my newly installed OpenSuSE 10.2, that is. Some things don't exist
>> in this version that I've come to depend on over the years, such as "lc"
>> and "uc" which do wholesale lowercase and uppercase filename
>> conversions. These are not installed (duh) and don't exist on the
>> installation media (downloaded CDs). Other stuff that IS installed also
>> doesn't work:
>>
> For uppercase to lowercase try this simple bash script
>
> #! /bin/bash
> #
> # Changes every filename in working directory to all lowercase.
> #
> # Inspired by a script of John Dubois,
> # which was translated into into Bash by Chet Ramey,
> # and considerably simplified by Mendel Cooper, author of this document.
>
>
> for filename in * # Traverse all files in directory.
> do
> fname=`basename $filename`
There is no need for an external command; bash can do it
internally:
fname=${filename##*/}
> n=`echo $fname | tr A-Z a-z` # Change name to lowercase.
That will fail if $fname has leading or trailing spaces.
> if [ "$fname" != "$n" ] # Rename only files not already lowercase.
> then
> mv $fname $n
That will fail if there are any spaces or other pathological
characters in the filename.
> fi
Rather than run all filenames through an external command, first
check whether any changes need to be made:
case $filename in
*[A-Z]*) n=`printf "%s\n" "$fname" | tr A-Z a-z`
mv -i -- "$fname" "$n"
;;
esac
You can speed up the script even more by using a dynamically
loadable builtin to change case. See my article at
<http://www.unixreview.com/documents/s=10089/ur0606a/ur0606a.htm>.
> done
>
> exit 0
>
> The original author is credited in the comments.
--
Chris F.A. Johnson <http://cfaj.freeshell.org>
===================================================================
Author:
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
More information about the QnA
mailing list