[mdlug] Hideous script for recursively renaming a directory tree to lower case
Robert Meier
list1c30fe42 at bellsouth.net
Tue Feb 5 22:58:57 EST 2008
Jeff,
> Thanks for the tip.
> I need to start using functions more in my scripts.
> It hasn't been a major problem yet due to my script
> structures but it would help readability if I planned for it in
> advance.
The situation of selecting files and performing some operation
crops up often enough to have it's own POSIX.2 idiom.
find ... -print | while read f; do ... "$f" ...; done
Note the use of quotes each parameter/command/... expansion
which avoids the often unintended splitting
of names with spaces into separate tokens.
Note that using one-pathname-per-line from -print allows use of "$f" in
dirname and other utilities in portable scripts.
---- function-find-while-test.sh ----
#!/bin/sh
# Project: Tutorials
# File: Demonstration of function, find, while use
# Type: /bin/sh
# Compl: POSIX.2
# Author: Dr. Robert J. Meier <robert.meier at computer.org>
# History: 2006.08.12 -rjm- file creation
# 2008.01.05 -rjm- added function demo
# First generate a function
changecase() {
FILENAME="$1" # quotes dont split words containing spaces
NEWFILENAME="$( echo "$FILENAME" |
tr "[:upper:][:lower:]" "[:lower:][:upper:]" )"
echo "$FILENAME => $NEWFILENAME"
mv "$FILENAME" "$NEWFILENAME"
}
export -f changecase # functions are not auto exported to subshells
# Create some files to change
TESTDIR=/tmp/test-function-find-while
test -e "$TESTDIR" && rm -r "$TESTDIR"
mkdir -p "$TESTDIR/bibbity/bobbity"
cd "$TESTDIR"
touch Abracabra bibbity/bobbity/boo 'Hocus Pocus'
# Change them using find-while idiom
find . -type f -print |
while read f; do (cd "$(dirname "$f")"; changecase "$(basename "$f")"); done
ls -R
---- function-find-while-test.sh ----
Hopefully helpful,
--
Bob
"You are responsible, forever, for what you have tamed.
You are responsible for your rose."
-- Antoine de Saint-Exupery
More information about the mdlug
mailing list