Р-р-рекомендованное название --- recase.
#!/bin/bash
set -e
set -u
ACTION='no'
FORCE='no'
UPPER='no'
SCRIPT=$(basename "$0")
usage ()
{
cat <<USAGE
Usage: $SCRIPT [options] files...
Renames files to lower or upper case.
Default is to rename to lower case.
Options are:
-A Do actual work ($ACTION)
-F Force renaming if files exist ($FORCE)
-u Rename to upper case instead of lower ($UPPER)
-h, -? This help message
Examples:
# $SCRIPT ee/*
mv "ee/RBwrwRW" "ee/rbwrwrw"
mv "ee/УМЦУУкцускцу" "ee/умцуукцускцу"
# $SCRIPT -u ee/*
mv "ee/RBwrwRW" "ee/RBWRWRW"
mv "ee/УМЦУУкцускцу" "ee/УМЦУУКЦУСКЦУ"
USAGE
exit 0
}
if [ $# == 0 ]; then
usage
fi
while getopts AFhu? opt; do
case $opt in
A) ACTION='yes';;
F) FORCE='yes';;
u) UPPER='yes';;
*) usage;;
esac
done
shift $(expr $OPTIND - 1)
if [ "$UPPER" == 'no' ]; then
sed_cmd='s/[[:upper:]]/\l&/g'
else
sed_cmd='s/[[:lower:]]/\u&/g'
fi
for p in "$@"; do
d=$(dirname "$p")
f=$(basename "$p")
n=$(echo "$f" | sed "$sed_cmd")
if [ "$ACTION" == 'yes' ]; then
if [ -e "$d/$n" ] && [ "$FORCE" != 'yes' ]; then
echo "File '$n' exists. Use -F to overwrite"
continue
else
mv "$d/$f" "$d/$n"
fi
else
echo "mv \"$d/$f\" \"$d/$n\""
fi
done
exit 0