Shell Script to make all uppercase or lowercase | Linux

Shell Scripting

Below script Transforms a file to all uppercase.

# Changes a file to all uppercase.
if [ -z "$1" ] # Standard check for command line arg.
echo "Usage: `basename $0` filename"
tr a-z A-Z <"$1"
# Same effect as above, but using POSIX character set notation:
# tr '[:lower:]' '[:upper:]' <"$1"
# Thanks, S.C.
exit 0


Below script Changes all filenames in working directory to lowercase.

#! /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.
fname=`basename $filename`
n=`echo $fname | tr A-Z a-z` # Change name to lowercase.
if [ "$fname" != "$n" ] # Rename only files not already lowercase.
mv $fname $n
exit 0
# 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.
for filename in * # Not necessary to use basename,
# since "*" won't return any file containing "/".
do n=`echo "$filename/" | tr '[:upper:]' '[:lower:]'`
# POSIX char set notation.
# Slash added so that trailing newlines are not
# removed by command substitution.
# Variable substitution:
n=${n%/} # Removes trailing slash, added above, from filename.
[[ $filename == $n ]] || mv "$filename" "$n"
# Checks if filename already lowercase.
exit 0

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.