//
you're reading...
bash

Script that change all filenames in a directory to upper/lower case

Usage: script.sh -c upper / script.sh -c lower
#!/bin/bash

DIRECTORY=prueba
#No arguments
if [ $# -eq 0 ];
then
 echo ""
 echo "Option -$OPTARG needs an argument." >&2
 echo ""
 exit 1
fi

while getopts ":c:*:" option; do
 case $option in
 c)
 if [[ $2 == "upper" ]];then
 for file in $(ls $DIRECTORY); do mv -- "$DIRECTORY/$file" "$DIRECTORY/${file^^}"; done
 echo "UPPER"
 elif [[ $2 == "lower" ]];then
 for file in $(ls $DIRECTORY); do mv -- "$DIRECTORY/$file" "$DIRECTORY/${file,,}"; done
 echo "LOWER"
 else
 echo "Invalid option"
 fi
 ;;

 \?)
 echo "Invalid option: -$OPTARG" >&2
 exit 1
 ;;
 🙂
 echo "Option -$OPTARG requires an argument." >&2
 exit 1
 ;;

 esac
done
Advertisements

About silvinux

I'm an IT freaky, that love to deploy (free software/ OpenSource) technologies in little projects at my home and this blog is a series of documents/manuals/guides that I've made through time to time.

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: