🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

tr Command

Intermediate Text Processing man(1)

Translate or delete characters

👁 11 views 📅 Updated: Mar 15, 2026
SYNTAX
tr [OPTION]... SET1 [SET2]

What Does tr Do?

The tr (translate) command replaces, deletes, or squeezes characters in text from standard input. It operates on individual characters (not strings), making it perfect for simple character-level transformations.

tr reads from stdin only — it cannot read files directly, so it is always used with pipes or redirection. Common uses include case conversion, removing unwanted characters, replacing delimiters, and cleaning up whitespace.

tr supports character classes like [:upper:], [:lower:], [:digit:], [:alpha:], and [:space:], which provide portable pattern matching across different locales and character sets.

Options & Flags

OptionDescriptionExample
-d Delete characters in SET1 echo 'hello 123' | tr -d '0-9'
-s Squeeze repeated characters to single occurrence echo 'hello world' | tr -s ' '
-c Complement SET1 (use all characters NOT in SET1) echo 'abc123' | tr -cd '0-9'
[:upper:] Character class: uppercase letters echo 'Hello' | tr '[:upper:]' '[:lower:]'
[:lower:] Character class: lowercase letters echo 'hello' | tr '[:lower:]' '[:upper:]'
[:digit:] Character class: digits 0-9 echo 'abc123' | tr -d '[:digit:]'

Practical Examples

#1 Convert to uppercase

Converts all lowercase letters to uppercase.
$ echo 'hello world' | tr '[:lower:]' '[:upper:]'
Output: HELLO WORLD

#2 Convert to lowercase

Converts all uppercase letters to lowercase.
$ echo 'HELLO' | tr '[:upper:]' '[:lower:]'
Output: hello

#3 Replace spaces with newlines

Splits space-separated words into separate lines.
$ echo 'one two three' | tr ' ' '\n'
Output: one two three

#4 Delete all digits

Removes all numeric characters from the input.
$ echo 'Order #12345' | tr -d '[:digit:]'
Output: Order #

#5 Squeeze multiple spaces

Collapses multiple consecutive spaces into a single space.
$ echo 'too many spaces' | tr -s ' '
Output: too many spaces

#6 Extract only digits

Keeps only digits and dots, removing everything else.
$ echo 'Price: $49.99' | tr -cd '0-9.'
Output: 49.99

#7 Replace characters

Replaces hyphens and underscores with spaces.
$ echo 'hello-world_test' | tr '-_' ' '
Output: hello world test

Tips & Best Practices

tr works on characters, not strings: tr 'abc' 'xyz' replaces a->x, b->y, c->z character by character. It does NOT replace the string 'abc' with 'xyz'. Use sed for string replacement.
Generate random strings: tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 32 generates a random 32-character alphanumeric string.
Stdin only: tr cannot read files directly. Use: tr ... < file.txt or cat file.txt | tr ... to process files.

Frequently Asked Questions

How do I convert text to uppercase in Linux?
Use tr '[:lower:]' '[:upper:]' to convert stdin to uppercase. For a file: tr '[:lower:]' '[:upper:]' < file.txt
How do I remove specific characters?
Use tr -d followed by the characters to remove: tr -d '[:digit:]' removes all digits. tr -d ' ' removes all spaces.
What is the difference between tr and sed?
tr translates individual characters (a->x, b->y). sed replaces strings and patterns. Use tr for character-level transforms and sed for string replacements.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →