tr - Translate and Transform Characters

Learn tr for character translation. Convert case, delete characters, decode ROT13 - all in one command.

commandsLast updated 2026-02-24

tr: The Character Translator

You need to convert a file to uppercase. Or delete all the digits. Or decode ROT13. tr does all of this by translating characters from one set to another.

echo "hello" | tr 'a-z' 'A-Z'

Output: HELLO. That's tr.

The Basic Pattern

tr 'SET1' 'SET2'

Every character in SET1 becomes the corresponding character in SET2. a becomes A, b becomes B, and so on.

Important: tr reads from stdin only. You always pipe into it.

Case Conversion

Lowercase to uppercase:

cat file.txt | tr 'a-z' 'A-Z'

Uppercase to lowercase:

cat file.txt | tr 'A-Z' 'a-z'

Deleting Characters

Use -d to delete instead of translate:

Remove all digits:

echo "abc123def" | tr -d '0-9'

Output: abcdef

Remove newlines:

cat file.txt | tr -d '\n'

Everything becomes one line.

Remove all non-letters:

echo "Hello, World! 123" | tr -d '[:punct:][:digit:][:space:]'

Output: HelloWorld

Squeezing Repeated Characters

Use -s to collapse repeated characters:

Multiple spaces to single space:

echo "too    many   spaces" | tr -s ' '

Output: too many spaces

ROT13 (The Classic)

ROT13 is a simple cipher that rotates letters by 13 positions:

echo "secret message" | tr 'a-zA-Z' 'n-za-mN-ZA-M'

Run it again to decode - ROT13 is its own inverse.

Character Classes

tr understands these shortcuts:

| Class | Meaning | |-------|---------| | a-z | Lowercase letters | | A-Z | Uppercase letters | | 0-9 | Digits | | [:lower:] | Lowercase letters | | [:upper:] | Uppercase letters | | [:digit:] | Digits | | [:space:] | Whitespace | | [:punct:] | Punctuation |

Real Examples

Normalize whitespace in messy data:

cat data.txt | tr -s ' \t' ' '

Remove carriage returns (Windows line endings):

cat file.txt | tr -d '\r'

Simple substitution cipher:

echo "hello" | tr 'a-z' 'zyxwvutsrqponmlkjihgfedcba'

Quick Reference

| What you want | Command | |---------------|---------| | Uppercase | tr 'a-z' 'A-Z' | | Lowercase | tr 'A-Z' 'a-z' | | Delete chars | tr -d '0-9' | | Squeeze repeats | tr -s ' ' | | ROT13 | tr 'a-zA-Z' 'n-za-mN-ZA-M' |

Practice

tr is surprisingly useful in CTF challenges, especially for decoding simple ciphers and cleaning up data.


tr is the underrated workhorse. Anytime you're doing character-level transforms, it's faster than sed and simpler than awk.