LDP/LDP/guide/docbook/abs-guide/lowercase.sh

44 lines
1.4 KiB
Bash
Raw Normal View History

2005-06-05 14:39:50 +00:00
#!/bin/bash
2001-07-10 14:25:50 +00:00
#
2005-06-05 14:39:50 +00:00
# Changes every filename in working directory to all lowercase.
2001-07-10 14:25:50 +00:00
#
2005-06-05 14:39:50 +00:00
# Inspired by a script of John Dubois,
#+ which was translated into Bash by Chet Ramey,
#+ and considerably simplified by the author of the ABS Guide.
2001-07-10 14:25:50 +00:00
2001-09-04 13:27:31 +00:00
for filename in * # Traverse all files in directory.
2001-07-10 14:25:50 +00:00
do
fname=`basename $filename`
2001-09-04 13:27:31 +00:00
n=`echo $fname | tr A-Z a-z` # Change name to lowercase.
if [ "$fname" != "$n" ] # Rename only files not already lowercase.
2001-07-10 14:25:50 +00:00
then
mv $fname $n
fi
done
2005-06-05 14:39:50 +00:00
exit $?
2001-07-10 14:25:50 +00:00
# Code below this line will not execute because of "exit".
#--------------------------------------------------------#
# To run it, delete script above line.
# The above script will not work on filenames containing blanks or newlines.
# Stephane Chazelas therefore suggests the following alternative:
2001-09-04 13:27:31 +00:00
for filename in * # Not necessary to use basename,
# since "*" won't return any file containing "/".
2001-07-10 14:25:50 +00:00
do n=`echo "$filename/" | tr '[:upper:]' '[:lower:]'`
2001-09-04 13:27:31 +00:00
# POSIX char set notation.
2001-07-10 14:25:50 +00:00
# Slash added so that trailing newlines are not
# removed by command substitution.
# Variable substitution:
2001-09-04 13:27:31 +00:00
n=${n%/} # Removes trailing slash, added above, from filename.
2001-07-10 14:25:50 +00:00
[[ $filename == $n ]] || mv "$filename" "$n"
2001-09-04 13:27:31 +00:00
# Checks if filename already lowercase.
2001-07-10 14:25:50 +00:00
done
2005-06-05 14:39:50 +00:00
exit $?