Linux command list

tr command: replace or delete characters

__ __ _ _____
\ \ / /__| |_|_ _|__ _ __ _ __ ___
\ \ /\ / / _ \ '_ \| |/ _ \ '__| '_ ` _ \
\ V V / __/ |_) | | __/ | | | | | |
\_/\_/ \___|_.__/|_|\___|_| |_| |_| |_
 
A sandbox for trying tr. Nothing here can touch your real files.
user@webterm:~/project$
 

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

Syntax

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.

Try it first

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

Changing the separator

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

Deleting and squeezing

-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.

When you actually reach for it

tr is what you type to shape text into what the next command can handle.

SituationWhat to type
Normalise to upper casetr '[:lower:]' '[:upper:]' < names.txt
Split a comma-separated linetr ',' '\n' < tags.txt
Squeeze repeated spacestr -s ' ' < messy.txt
Join everything onto one linetr -d '\n' < list.txt
Turn spaces into underscorestr ' ' '_'

Things that trip people up

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.

Practise it hands-on

webterm.appthis site

learn.webterm.appa separate site

>_WEBTERM LEARN

WebTerm Learn: from one command to actually using it

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

Related commands

sed / cut / sort / uniq

Frequently asked questions

Can I pass a file name to tr?
No. tr reads standard input only, so feed it with `tr 'a-z' 'A-Z' < file` or pipe it as `cat file | tr ...`.
How is it different from sed?
tr replaces **single characters**, sed replaces strings. Mapping `abc` onto `xyz` one character at a time is tr; turning `hello` into `hi` is sed.
What do -d and -s do?
`-d` deletes the characters you list, and `-s` squeezes runs of the same character down to one. `-s ' '` is the usual way to clean up spacing.
What is `[:lower:]`?
A character class, the same set as `a-z` but easier to read. `[:upper:]`, `[:digit:]` and `[:space:]` exist too.
How do I write a newline?
As `'\n'`, and a tab as `'\t'`. Splitting a comma-separated line is `tr ',' '\n'`.