tr: Translate Characters
tr (translate) replaces, deletes or squeezes individual characters. It reads only from its input (a pipe or a redirect, never a file name argument) and works character by character, which makes it perfect for case conversion, cleaning up separators and stripping unwanted characters.
Replacing characters
tr SET1 SET2 replaces each character of SET1 with the character at the same position in SET2:
echo "hello world" | tr 'a-z' 'A-Z'
echo "2026-03-15" | tr '-' '/'
echo "a,b,c" | tr ',' '\n'
echo "path:to:file" | tr ':' ' 'HELLO WORLD
2026/03/15
a
b
c
path to file
a-z is a range. Escapes such as \n (newline) and \t (tab) work too, so tr ',' '\n' puts each item on its own line.
Character classes
Instead of ranges you can use POSIX classes, which are clearer and handle locales properly:
| Class | Characters |
|---|---|
[:lower:] / [:upper:] |
Lower / upper case letters |
[:alpha:] |
Letters |
[:digit:] |
Digits 0-9 |
[:alnum:] |
Letters and digits |
[:space:] |
Whitespace: space, tab, newline … |
[:punct:] |
Punctuation |
echo "Release Notes v2" | tr '[:upper:]' '[:lower:]'
echo "Order #4521 ships in 3 days" | tr -d '[:digit:]'
echo "Hello, World! (draft)" | tr -d '[:punct:]'release notes v2
Order # ships in days
Hello World draft
Deleting with -d
-d deletes every character in the set. A common use is cleaning Windows line endings (\r) from a file:
printf 'id,name\r\n1,Ada\r\n' > windows.csv
cat -A windows.csv
tr -d '\r' < windows.csv > unix.csv
cat -A unix.csv
echo "phone: +44 (20) 7946-0958" | tr -cd '[:digit:]\n'id,name^M$
1,Ada^M$
id,name$
1,Ada$
442079460958
-c (complement) inverts the set: tr -cd '[:digit:]\n' deletes everything that is not a digit or newline, leaving only the phone number's digits.
Squeezing with -s
-s replaces runs of the same character with a single one:
echo "too many spaces" | tr -s ' '
printf 'line1\n\n\n\nline2\n' | tr -s '\n'
echo "a---b--c" | tr -s '-' ' 'too many spaces
line1
line2
a b c
The last line both translates and squeezes: dashes become spaces and runs are collapsed. tr -s ' ' before cut -d' ' fixes the "multiple spaces" problem from the cut lesson.
A word-frequency counter
tr is often the first step in a text pipeline: split text into one word per line, normalise the case, then count:
cat > speech.txt <<'TXT'
The shell is a tool. The shell reads commands;
the commands run tools, and tools make the shell useful.
TXT
tr -cs '[:alpha:]' '\n' < speech.txt | tr '[:upper:]' '[:lower:]' | sort | uniq -c | sort -k1,1rn -k2,2 | head -n 4 4 the
3 shell
2 commands
2 tools
tr -cs '[:alpha:]' '\n' turns every run of non-letters (spaces, punctuation, newlines) into a single newline, leaving one word per line.
A classic: ROT13
Because tr maps sets position by position, you can implement simple ciphers in one line. ROT13 shifts every letter 13 places, and applying it twice gives the original back:
echo "Linux is fun" | tr 'A-Za-z' 'N-ZA-Mn-za-m'
echo "Linux is fun" | tr 'A-Za-z' 'N-ZA-Mn-za-m' | tr 'A-Za-z' 'N-ZA-Mn-za-m'Yvahk vf sha
Linux is fun
Limits of tr
- It works on single characters:
tr 'cat' 'dog'maps c→d, a→o, t→g everywhere, it does not replace the word "cat". Usesed 's/cat/dog/g'for words. - It doesn't take file names:
tr a b file.txtis an error. Writetr a b < file.txt. - In GNU
tr, multibyte UTF-8 characters such aséare not handled as single characters.
Test yourself
Which command converts text to upper case?
- tr -u
- tr 'a-z' 'A-Z'
- tr -s 'a-z'
- tr -d 'A-Z'
What does echo 'a b c' | tr -s ' ' print?
abca b ca b ca\nb\nc
What does echo cat | tr 'cat' 'dog' print?
- dog
- cat
- An error
- dgo