Shell Script To Rename File Name To Lowercase

in Categories File-management last updated September 16, 2008

This script use tr command to convert uppercase file name to a lowercase file name. The tr utility copies the given input to produced the output with substitution or deletion of selected characters. tr abbreviated as translate or transliterate. It takes as parameters two sets of characters, and replaces occurrences of the characters in the first set with the corresponding elements from the other set i.e. it is used to translate characters.

#!/bin/bash
# Write a shell script that changes the name of the file passed as argument
# to lowercase.
# -------------------------------------------------------------------------
# Copyright (c) 2003 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
file="$1"
if [ $# -eq 0 ]
then
	echo "$(basename $0) file"
	exit 1
fi
 
if [ ! $file ]
then
	echo "$file not a file"
	exit 2
fi
 
lowercase=$(echo $file | tr '[A-Z]' '[a-z]'])
 
if [ -f $lowercase ]
then
	echo "Error - File already exists!"
	exit 3
fi
 
# change file name
/bin/mv $file $lowercase

Share this on: