Use ‘tr’ in a shell script to translate text case
This tip is applicable to UNIX / Linux operating systems
‘tr’ is a command line utility which you can use to convert text to UPPER or lower case.
To convert all the letters of the English alphabet found in a string to lower case do this at the UNIX command line:
% sh (to switch to bourne shell)
% string="AbbATTTbb"
% echo $string | tr ′[A-Z]′ ′[a-z]′
abbatttbb
Pretty simple eh? Now say you have a list of images in a folder and they are all mixed up and you want to get them synchronized. You could use a simple script to do this. Call it ‘convert_to_lowercase.sh’. The contents are:
#!/bin/sh
folder="/home/my/images"
cd $folder
list=`ls`
for file in $list
do
new_name=`echo $file | tr ′[A-Z]′ ′[a-z]′`
mv $file $new_name
done
To run the script from the command line enter:
% sh convert.sh
You can also chmod 777 convert.sh if you wish.
Tags: shell script, text translation tr


