File Viewing & Editing

tr

Translate, squeeze, or delete characters from standard input.

Synopsis

syntax
tr [OPTION]... SET1 [SET2]

Examples

Convert to uppercase
echo 'hello' | tr 'a-z' 'A-Z'
Remove all newlines
tr -d '\n' < file.txt
Squeeze repeated spaces into one
tr -s ' ' < file.txt
Generate random alphanumeric string
tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c 16

Common options

FlagDescription
-dDelete characters in SET1
-sSqueeze repeated characters
-c, -CComplement SET1

About tr

The `tr` command translate, squeeze, or delete characters from standard input. Text viewing and editing commands are fundamental tools in any Linux user's toolkit.

Linux treats almost everything as a file, so the ability to quickly inspect, filter, transform, and edit file contents from the command line is critical. These commands are regularly combined with pipes and redirects to build powerful data-processing pipelines.

The command accepts 3 commonly used flags shown above, though the full set of options is available in the man page (`man tr`). The 4 examples on this page cover typical real-world usage patterns that you can copy and adapt for your own workflows.

Related commands

More File Viewing & Editing Commands

Other commands in the File Viewing & Editing category

Related tools