Converting between uppercase and lowercase on the Linux command line (2024)

Converting between uppercase and lowercase on the Linux command line (1)

bySandra Henry Stocker

Unix Dweeb

How-To

Jun 07, 20245 mins

Linux

Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy.

Converting between uppercase and lowercase on the Linux command line (2)

Credit: Daboost/Shutterstock

There are many ways to change text on the Linux command line from lowercase to uppercase and vice versa. In fact, you have an impressive set of commands to choose from. This post examines some of the best commands for the job and how you can get them to do just what you want.

Using tr

The tr (translate) command is one of the easiest to use on the command line or within a script. If you have a string that you want to be sure is in uppercase, you just pass it through a tr command like this:

$ echo Hello There | tr [:lower:] [:upper:]HELLO THERE

Below is an example of using this kind of command in a script when you want to be sure that all of the text that is added to a file is in uppercase for consistency:

#!/bin/bashecho -n "Enter department name: "read deptecho $dept | tr [:lower:] [:upper:] >> depts

Switching the order to [:upper:] [:lower:] would have the opposite effect, putting all the department names in lowercase:

echo $dept | tr [:upper:] [:lower:] >> depts

Similarly, you could use the sed command’sA-Z and a-z strings to accomplish the same thing:

echo $dept | tr a-z A-Z >> depts

As you undoubtedly suspect, reversing the order of the a-z and A-Z strings will have the opposite effect, turning the text to all lowercase.

Using awk

The awk command lets you do the same thing with itstoupper and tolower options. The command in the script shown in the previous example could be done this way instead:

echo $dept | awk '{print toupper($0)}' >> depts

The reverse (switching to lowercase) would look like this:

echo $dept | awk '{print tolower($0)}' >> depts

Using sed

The sed (stream editor) command also does a great job of switching between upper- and lowercase. This command would have the same effect as the first of the two shown above.

echo $dept | sed 's/[a-z]/\U&/g' >> depts

Switching from uppercase to lowercase would simply involve replacing the Unear the end of the line with an L.

echo $dept | sed 's/[A-Z]/\L&/g' >> depts

Manipulating text in a file

Both awk and sed also allow you to change the case of text for entire files. So, you just found out your boss wanted those department names in all lowercase? No problem. Just run a command like this with the file name provided:

$ awk '{print tolower($0)}' deptsfinancebillingbookkeeping

If you want to overwrite the depts file, instead of just displaying its contents in lowercase, you would need to do something like this:

$ awk '{print tolower($0)}' depts > depts-$ mv depts- depts

Making the change with sed, however, you can avoid that last step because sed can edit a file “in place” as shown here, leaving the file intact, but the text in all lowercase:

$ sed 's/[A-Z]/\L&/g' depts

Capitalizing first letters only

To capitalize only the first letters of words in a string, you can do something like this:

$ echo design \& engineering | sed -e "s/\b\(.\)/\u\1/g"Design & Engineering

That command will ensure that first letters are capitalized, but won’t change the rest of the letters.

Making sure only first letters are uppercase

It’s a little more challenging when you want to change text so that only first letters are in uppercase. Say you’re manipulating a list of staff members’ names and you want them to be formatted in the normal Firstname Lastname manner.

with sed

You could use a considerably more complex sed command to ensure this result:

$ echo design & ENGINEERING | sed 's/b([[:alpha:]])([[:alpha:]]*)b/u1L2/g'Design & Engineering

with python

If you have python loaded, you can run a command like this that also formats text so that only the first letters of each word are capitalized and the command may be a little easier to parse than the sed command shown above:

$ echo -n "design & engineering" | python3 -c "import sys; print(sys.stdin.read().title())"Design & Engineering

There are many ways to change the formatting of text between upper- and lowercase. Which works best depends in part of whether you’re manipulating a single string or an entire file and how you want the end result to look.

Related content

  • how-toPipe viewer: Using the pv command on Linux The pv command is especially helpful when you are running tasks that take a long time to complete and you want some feedback that displays your progress.BySandra Henry StockerJun 04, 20243 minsLinux
  • how-toBackgrounding and foregrounding processes in the Linux terminal Running processes in the background can be convenient when you want to use your terminal window for something else while you wait for the first task to complete. BySandra Henry StockerMay 24, 20245 minsLinux
  • how-toMaking a case for case statements on Linux Case statements can allow you to simplify the logic of your scripts.BySandra Henry StockerMay 22, 20244 minsLinux
  • how-toCompressing files using the zip command on Linux The zip command lets you compress files to preserve them or back them up, and you can require a password to extract the contents of a zip file.BySandra Henry StockerMay 13, 20244 minsLinux
  • PODCASTS
  • VIDEOS
  • RESOURCES
  • EVENTS

NEWSLETTERS

Newsletter Promo Module Test

Description for newsletter promo module.

Converting between uppercase and lowercase on the Linux command line (3)

by Sandra Henry Stocker

Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

Most popular authors

  • Converting between uppercase and lowercase on the Linux command line (4)

    Anirban Ghoshal

    Senior Writer

  • Converting between uppercase and lowercase on the Linux command line (5)

    Maria Korolov

    Contributing writer

  • Converting between uppercase and lowercase on the Linux command line (6)

    Maria Korolov

    Contributing writer

Show me more

news Cisco steps up full-stack observability play with Splunk tie-ins By Michael CooneyJun 07, 20245 mins Network Management SoftwareNetworking
how-to Converting between uppercase and lowercase on the Linux command line By Sandra Henry StockerJun 07, 20245 mins Linux
news Network hiring, skills and certification trends By Denise DubieJun 06, 202412 mins CareersData CenterNetworking
podcast Has the hype around ‘Internet of Things’ paid off? | Ep. 145 Apr 18, 202436 mins IoT PlatformsIoT Security
podcast Episode 1: Understanding Cisco’s Converged SDN Transport Sep 24, 202120 mins Cisco SystemsInternetNetworking
podcast Episode 2: Pluggable Optics and the Internet for the Future Sep 23, 202117 mins Optical DrivesCisco SystemsInternet
video Has the hype around ‘Internet of Things’ paid off? Apr 18, 202436 mins IoT PlatformsInternet of Things
video Are unused IPv4 addresses a secret gold mine? Jan 11, 202435 mins IPv6Network Management SoftwareNetworking
video Preparing for a 6G wireless world: Exciting changes coming to the wireless industry Nov 07, 202330 mins 5GShort-range Wireless
Converting between uppercase and lowercase on the Linux command line (2024)
Top Articles
Latest Posts
Article information

Author: Stevie Stamm

Last Updated:

Views: 5759

Rating: 5 / 5 (60 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Stevie Stamm

Birthday: 1996-06-22

Address: Apt. 419 4200 Sipes Estate, East Delmerview, WY 05617

Phone: +342332224300

Job: Future Advertising Analyst

Hobby: Leather crafting, Puzzles, Leather crafting, scrapbook, Urban exploration, Cabaret, Skateboarding

Introduction: My name is Stevie Stamm, I am a colorful, sparkling, splendid, vast, open, hilarious, tender person who loves writing and wants to share my knowledge and understanding with you.