Linux Bash Convert Lower to Upper

Linux Bash Command Line — Convert lower case characters to upper case or the opposite!

So lets say your an anal retentive guy like me and the format of certain things is very important to you as well as key organization of various files which are dated or what I would call a TIMESTAMP. I have many scripts that run and these scripts have log files so in the event of an error I can trace the scripts steps to locate the issue thus requiring a date or TIMESTAMP attached to each file.

Below is sample code that will convert your string from lower case to upper: (this method assumes you have “tr” installed on your host machine – it is “Translate Characters”)

#!/bin/bash
TIMESTAMP=`date +%G%b%d_%H%M`
LOGTIME=`echo $TIMESTAMP | tr “[:lower:]” “[:upper:]”`
echo -e ‘\n———–‘
echo $LOGTIME

The above code will echo a string as follows:

YYYYMMMDD__hhmm

(predefined by the date command, run “date –help” at your command line for more info)
Y = year, digit
M = month, character
D = day of the month, digit
h = hour, digit
m = minute, digit

So my file might look like FULL_SYS_BAK_2009FEB03_2330.log, that way all my files will be consistently named and organized… When you do your “ls -l” to list your directory and everything is organized its much easier on the eye(s).

$ ls -l
FULL_SYS_BAK_2009JAN28_2330.log
FULL_SYS_BAK_2009JAN29_2330.log
FULL_SYS_BAK_2009JAN30_2330.log
FULL_SYS_BAK_2009JAN31_2330.log
FULL_SYS_BAK_2009FEB01_2330.log
FULL_SYS_BAK_2009FEB02_2330.log
FULL_SYS_BAK_2009FEB03_2330.log

The above looks much better than lower case if you ask me, but where it really matters is when you have multiple files with different names! I usually will start my file names with the date rather then the intended file name, it is just easier to narrow/track down the file you need.

I was able to derive this method from the following site(s): Convert to Upper/Lower Function. Please also refer to the following link for the tr command. Last but not least you may also want to look into the date man pages. If any of these links are broken please report them, thank you!

[ad#google-468×60]

Leave a Reply

Your email address will not be published. Required fields are marked *

*