tr translates and deletes characters. Learn how to change case, how to turn a separator into newlines, and how `-s` squeezes runs of spaces, by running the commands in a real terminal in your browser.
Updated: 2026-09-06
tr from to < file
tr -d characters < file
It works character by character.
It takes no file name, so feed it with < or a pipe.
Upper-case some text.
$ cat names.txt
Ada Lovelace
Linus Torvalds
Grace Hopper
$ tr 'a-z' 'A-Z' < names.txt
ADA LOVELACE
LINUS TORVALDS
GRACE HOPPER
Each character of a-z maps to the one in the same position of A-Z.
A pipe does the same.
$ cat names.txt | tr 'a-z' 'A-Z'
ADA LOVELACE
LINUS TORVALDS
GRACE HOPPER
Character classes save you writing ranges.
$ tr '[:lower:]' '[:upper:]' < names.txt
ADA LOVELACE
LINUS TORVALDS
GRACE HOPPER
The common use is opening one packed line into many.
$ cat tags.txt
web,infra,db,web
$ tr ',' '\n' < tags.txt
web
infra
db
web
Once it is one item per line, counting is easy.
$ tr ',' '\n' < tags.txt | sort | uniq -c
1 db
1 infra
2 web
-d removes the characters you name.
$ tr -d ' ' < names.txt
AdaLovelace
LinusTorvalds
GraceHopper
-s squeezes repeats into one.
Logs padded with spaces to line up become workable again.
$ cat messy.txt
log entry one
log entry two
$ tr -s ' ' < messy.txt
log entry one
log entry two
After that, cut -d' ' can pick out fields.
tr is what you type to shape text into what the next command can handle.
| Situation | What to type |
|---|---|
| Normalise to upper case | tr '[:lower:]' '[:upper:]' < names.txt |
| Split a comma-separated line | tr ',' '\n' < tags.txt |
| Squeeze repeated spaces | tr -s ' ' < messy.txt |
| Join everything onto one line | tr -d '\n' < list.txt |
| Turn spaces into underscores | tr ' ' '_' |
It does not take a file name.
$ tr 'a-z' 'A-Z' names.txt
tr: extra operand ‘names.txt’
Use < or a pipe instead, because tr reads standard input and nothing else.
The mapping is character by character.
tr 'abc' 'xyz' turns a into x, b into y and c into z.
It does not look for the sequence abc.
For that, use sed.
A shorter second set repeats its last character.
$ echo hello | tr 'a-z' 'X'
XXXXX
All 26 letters were mapped onto X.
Matching lengths is the safer habit.
webterm.appthis site
Terminal Fundamentals
Learn commonly used commands
learn.webterm.appa separate site

Commands stick when they show up in a real sequence of work, not one at a time. There is a course that builds them up in order.
See the course