tr Command — Translating and Transforming Text in Linux¶
The
tr(translate) command is used to transform, replace, delete, and compress characters from standard input. It is one of the most useful Linux text-processing utilities for formatting data, cleaning text, converting letter cases, and preparing data for scripts and automation.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand the
trcommand - Replace characters
- Convert lowercase to uppercase
- Convert uppercase to lowercase
- Delete characters
- Remove repeated characters
- Process command output
- Use
trin shell pipelines
Prerequisites¶
Before starting this lesson, complete:
- Module 1 – Linux Fundamentals
- Module 2 – Linux Command Line Essentials
- Module 3 Lessons 1–5
Why Learn tr?¶
Imagine you receive inconsistent user input.
You want everything in uppercase.
Instead of editing manually:
Output:
This is a common task in automation and shell scripting.
What is tr?¶
tr stands for:
Translate
It reads text from Standard Input (stdin) and transforms it.
Unlike commands such as cat or grep, tr does not read files directly.
Syntax:
Most commonly used with pipes:
or
Character Translation¶
Replace one character with another.
Output:
Replace spaces with underscores.
Output:
Convert Lowercase to Uppercase¶
Output:
Another example:
Output:
Convert Uppercase to Lowercase¶
Output:
Using POSIX Character Classes¶
Instead of ranges:
Use:
Convert to lowercase:
POSIX character classes are more portable across different locales.
Delete Characters¶
Remove vowels.
Output:
Remove spaces.
Output:
Remove digits.
Output:
Squeeze Repeated Characters¶
Suppose:
Compress repeated spaces.
Output:
Compress repeated blank lines.
Replace Tabs¶
Convert tabs to spaces.
Replace Colons¶
Example:
Command:
Output:
One Character Per Line¶
Display every character on its own line.
A more common approach is:
Combining with Pipes¶
Convert usernames to uppercase.
Remove spaces.
Count words.
Convert log entries.
Common tr Options¶
| Option | Description |
|---|---|
-d | Delete characters |
-s | Squeeze repeated characters |
-c | Complement character set |
-t | Truncate SET1 to match SET2 |
Real Production Examples¶
Convert usernames.
Remove spaces from configuration values.
Normalize CSV delimiters.
Clean log output.
Prepare environment variables.
Production Perspective¶
The tr command is commonly used for:
- Data cleanup
- Case conversion
- Formatting reports
- Removing unwanted characters
- Normalizing input
- Preparing text for shell scripts
It is lightweight, fast, and works exceptionally well in command pipelines.
Hands-on Lab¶
Task 1¶
Convert to uppercase.
Task 2¶
Convert to lowercase.
Task 3¶
Replace spaces.
Task 4¶
Delete vowels.
Task 5¶
Remove digits.
Task 6¶
Compress spaces.
Task 7¶
Convert usernames.
Task 8¶
Replace commas.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
tr 'a-z' 'A-Z' | Uppercase conversion | Normalize input |
tr 'A-Z' 'a-z' | Lowercase conversion | Standardize logs |
tr -d | Delete characters | Remove spaces or digits |
tr -s | Compress repeats | Clean formatted reports |
tr ':' ',' | Replace delimiters | Convert data formats |
Production Troubleshooting Scenario¶
Scenario
An operations team receives a user report with inconsistent formatting.
Tasks:
- Convert all names to uppercase.
- Remove blank spaces.
- Replace commas with semicolons.
- Compress multiple spaces into one.
- Remove numeric characters from usernames.
Solutions:
cat users.txt | tr '[:lower:]' '[:upper:]'
cat users.txt | tr -d ' '
cat users.txt | tr ',' ';'
cat users.txt | tr -s ' '
cat users.txt | tr -d '0-9'
Mini Challenge¶
Create:
Contents:
Perform the following:
- Convert all text to uppercase.
- Convert all text to lowercase.
- Remove digits.
- Replace spaces with underscores.
- Remove repeated spaces.
- Replace underscores back with spaces.
Best Practices¶
- Remember that
trreads from standard input, not directly from files. - Prefer POSIX character classes (
[:upper:],[:lower:]) for portability. - Use
tr -sto clean repeated delimiters. - Combine
trwithcut,grep,sort, andawkfor efficient text processing. - Test transformations on sample data before using them in production scripts.
Common Mistakes¶
❌ Trying to use tr directly on a file.
✅ Incorrect:
Correct:
or
❌ Expecting tr to replace words.
✅ tr works on individual characters, not words.
Use sed for word replacement.
❌ Forgetting that tr preserves line structure.
✅ It transforms characters but does not rearrange lines.
Interview Questions¶
Beginner¶
- What does the
trcommand do? - How do you convert lowercase to uppercase?
- Which option deletes characters?
- What does
tr -sdo?
Intermediate¶
- Why does
trwork with standard input instead of files? - What is the advantage of using POSIX character classes?
- How do you replace commas with tabs?
- When should you use
trinstead ofsed?
Architect Level¶
- How would you normalize inconsistent log data before analysis?
- Why is
trfrequently used in shell scripts and CI/CD pipelines? - How can
trimprove data quality in automation workflows?
Summary¶
In this lesson, you learned:
- Character translation
- Case conversion
- Character deletion
- Character replacement
- Squeezing repeated characters
- Using
trin command pipelines - Production text transformation techniques
The tr command is a fast and efficient tool for character-level transformations. It is widely used in Linux administration, automation, and shell scripting to clean and normalize text.
Key Takeaways¶
trperforms character-by-character transformations.- It reads input from stdin.
- Use
-dto delete characters. - Use
-sto squeeze repeated characters. - Prefer POSIX character classes for portability.
- Use
sedwhen you need to replace words or patterns instead of individual characters.
What's Next?¶
wc Command — Counting Lines, Words, Characters, and Bytes in Linux
In the next lesson, you'll learn:
- Counting lines
- Counting words
- Counting characters
- Counting bytes
- Combining
wcwith pipes - Real-world reporting and log analysis