Quick take: The tr command translates or deletes characters from input. Convert case with tr 'a-z' 'A-Z', delete characters with -d, and squeeze repeats with -s. It reads from standard input, so it lives in pipelines.

Introduction

The tr command (translate) maps one set of characters to another, deletes characters, or squeezes repeated ones. It works on streams rather than files, so it is almost always used in a pipeline to clean or transform text — changing case, stripping unwanted characters, or normalising whitespace.

This guide covers translating sets, deleting and squeezing characters, and the common text-cleaning recipes tr makes trivial.

Syntax

The basic syntax of the tr command is:

tr [OPTIONS] SET1 [SET2]

Common Options and Parameters

The most useful options and parameters for the tr command:

OptionDescription
(default)Translate characters in SET1 to those in SET2.
-dDelete characters in SET1.
-sSqueeze repeated characters in SET1 into one.
-cComplement SET1 (operate on characters NOT in it).
[:upper:] etc.Character classes like [:alpha:], [:digit:], [:space:].

Practical Examples

Real tr commands you can run today:

# Convert lowercase to uppercase
echo 'hello' | tr 'a-z' 'A-Z'
# Convert using character classes
tr '[:lower:]' '[:upper:]' < file.txt
# Delete all digits
echo 'abc123' | tr -d '0-9'
# Squeeze repeated spaces into one
tr -s ' ' < messy.txt
# Replace newlines with commas
tr '\n' ',' < list.txt
# Keep only letters and numbers
tr -cd '[:alnum:]\n' < data.txt

Tips and Best Practices

  • tr reads from standard input, so feed it with a pipe or input redirection (tr ... < file) — it does not take a filename argument.
  • Use character classes like [:upper:] and [:digit:] for clarity and correctness across locales.
  • tr -s ' ' is a quick way to collapse multiple spaces, handy before processing with cut.

Final Thoughts

tr is a focused, fast filter for character-level text surgery — case conversion, deletion, and squeezing. Because it works on streams, it slots naturally into pipelines for cleaning data before tools like cut or sort. For anything word- or line-level, reach for sed or awk instead.

FAQ: tr Command in Linux

How do I convert text to uppercase in Linux?+

Pipe it through tr: echo 'text' | tr 'a-z' 'A-Z', or use the clearer class form tr '[:lower:]' '[:upper:]'. Reverse the sets to convert to lowercase.

How do I delete specific characters with tr?+

Use -d with the set to remove: echo 'a1b2' | tr -d '0-9' deletes all digits. tr -d '[:punct:]' strips punctuation.

How do I remove duplicate spaces with tr?+

Use -s (squeeze): tr -s ' ' collapses runs of spaces into a single space. This is handy for cleaning up irregular whitespace.

Why does tr not accept a filename?+

tr only reads from standard input. Give it data with a pipe (command | tr …) or input redirection (tr … < file) rather than a filename argument.

What is the difference between tr and sed?+

tr operates on individual characters — translating, deleting, or squeezing them — and is very fast. sed works on patterns and whole lines, so use sed for find-and-replace of words or strings and tr for character-level changes.

Need help with Linux servers or infrastructure?

Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.

Hire Me for Support