tr Command in Linux: Translate, Delete, and Squeeze Characters

tr is a command-line utility in Linux and Unix systems that translates, deletes, and squeezes characters from the standard input and writes the result to the standard output.
The tr command can perform operations like removing repeated characters, converting uppercase to lowercase, and replacing and removing characters. Typically, it is used in combination with other commands through piping.
This guide explains how to use the tr command through practical examples and detailed explanations of the most common options.
tr Command Syntax
The syntax for the tr command is as follows:
tr [OPTIONS] SET1 [SET2]tr accepts two sets of characters, usually of the same length, and replaces each character from SET1 with the corresponding character from SET2.
A SET is a string of characters, including special backslash-escaped characters.
In the following example, tr replaces all characters from the standard input by mapping each character in SET1 to the matching one in SET2:
echo 'linuxize' | tr 'lin' 'red'Each occurrence of l is replaced with r, i with e, and n with d:
reduxezeCharacter sets can also be defined using ranges. For example, instead of writing:
echo 'linuxize' | tr 'lmno' 'wxyz'you can use:
echo 'linuxize' | tr 'l-n' 'w-z'When the -c (--complement) option is used, tr replaces all characters that are not in SET1.
In the following example, all characters except li are replaced with the last character from SET2:
echo 'linuxize' | tr -c 'li' 'xy'liyyyiyyyThe output has one more visible character than the input because the echo
command appends an invisible newline character \n that is also replaced with y. To print a string without a trailing newline, use the -n option.
The -d (--delete) option tells tr to delete characters specified in SET1. When deleting characters without squeezing, specify only one set.
The following command removes l, i, and z characters:
echo 'Linuxize' | tr -d 'liz'The L character is not deleted because the input includes an uppercase L while the character in the set is lowercase l.
LnuxeThe -s (--squeeze-repeats) option replaces a sequence of repeated characters with a single occurrence of that character.
In the following example, tr removes the repeated space characters:
echo "GNU \ Linux" | tr -s ' 'GNU \ LinuxWhen SET2 is specified, the repeated characters from SET1 are replaced with the single character from SET2:
echo "GNU \ Linux" | tr -s ' ' '_'GNU_\_LinuxThe -t (--truncate-set1) option forces tr to truncate SET1 to the length of SET2 before processing.
By default, if SET1 is longer than SET2, tr reuses the last character of SET2. Here is an example:
echo 'Linux ize' | tr 'abcde' '12'The character e from SET1 is matched with the last character of SET2, which is 2:
Linux iz2Using the same command with -t:
echo 'Linux ize' | tr -t 'abcde' '12'Linux izeThe last three characters of SET1 are discarded. SET1 becomes ab, the same length as SET2, and no replacement is made since the input contains none of those characters.
Combining Options
The tr command allows you to combine options. For example, the following command first replaces all characters except i with 0, then squeezes the repeated 0 characters:
echo 'Linux ize' | tr -cs 'i' '0'0i0i0tr Command Examples
In this section, we will cover practical examples of common uses of the tr command.
Convert Lowercase to Uppercase
Converting case is one of the most typical uses of tr. [:lower:] matches all lowercase characters and [:upper:] matches all uppercase characters:
echo 'Linuxize' | tr '[:lower:]' '[:upper:]'LINUXIZEInstead of character classes, you can use ranges:
echo 'Linuxize' | tr 'a-z' 'A-Z'To convert uppercase to lowercase, swap the positions of the two sets.
Remove All Non-Numeric Characters
The following command removes all non-numeric characters:
echo "my phone is 123-456-7890" | tr -cd '[:digit:]'[:digit:] matches all digit characters. The -c option inverts the set, and -d deletes the matched characters, leaving only digits:
1234567890Put Each Word on a New Line
To place each word on its own line, replace all non-alphanumeric characters with a newline:
echo 'GNU is an operating system' | tr -cs '[:alnum:]' '\n'GNU
is
an
operating
systemRemove Windows Line Endings
Windows text files use \r\n (carriage return + newline) as line endings. To convert a Windows file to Unix format, delete the carriage return characters:
tr -d '\r' < windows.txt > unix.txtThis is one of the most common real-world uses of tr when working with files transferred from Windows systems.
Remove Blank Lines
To remove blank lines from a file, squeeze repeated newline characters:
tr -s '\n' < file.txt > new_file.txtThe redirection < passes the contents of file.txt to tr. The > writes the output to new_file.txt.
Print $PATH Directories on Separate Lines
The $PATH environment variable
is a colon-delimited list of directories the shell searches for executables.
To print each directory on its own line, replace the colons with newlines:
printf '%s\n' "$PATH" | tr ':' '\n'/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/binFor related field and stream processing patterns, see the cut command guide
.
Quick Reference
| Command | Description |
|---|---|
tr SET1 SET2 | Replace characters in SET1 with SET2 |
tr -d SET1 | Delete characters in SET1 |
tr -s SET1 | Squeeze repeated characters to one |
tr -c SET1 SET2 | Replace characters NOT in SET1 |
tr -cd SET1 | Delete characters NOT in SET1 |
tr '[:lower:]' '[:upper:]' | Convert to uppercase |
tr '[:upper:]' '[:lower:]' | Convert to lowercase |
tr -d '\r' | Remove Windows carriage returns |
tr -s ' ' | Squeeze multiple spaces to one |
tr ':' '\n' | Replace colons with newlines |
Troubleshooting
tr does not edit files in placetr reads from standard input and writes to standard output only. To process a file, use input and output redirection: tr 'a-z' 'A-Z' < input.txt > output.txt. Do not redirect to the same file in one command — use a temporary file, then replace the original.
Output contains unexpected extra characters when using -c
The complement set includes the trailing newline character added by echo. Use echo -n to suppress the newline, or pipe through tr -d '\n' after your main tr command.
tr does not handle UTF-8 or multi-byte characterstr operates on single bytes. It does not correctly handle multi-byte UTF-8 characters such as accented letters or non-Latin scripts. For multi-byte character translation, use sed
or iconv instead.
FAQ
Does tr support regular expressions?
No. tr works with character sets and ranges, not regular expressions. For regex-based substitution, use sed
or awk
.
Can tr edit files in place?
No. tr only reads from standard input and writes to standard output. Use redirection to read from and write to files. Never redirect both input and output to the same file in a single command — the file will be truncated before it is read.
Does tr support UTF-8 or multi-byte characters?
GNU tr on Linux operates on bytes, not characters. Multi-byte UTF-8 sequences are not handled correctly. Use sed or iconv for Unicode-aware character translation.
What is the difference between tr and sed?tr translates or deletes individual characters and is simpler and faster for single-character operations. sed supports regular expressions and can match and replace patterns of any length. Use tr for character-level work and sed for pattern-based substitution.
Why do my translated characters not match what I expected?
Check whether the lengths of SET1 and SET2 match. If SET2 is shorter, the last character of SET2 is reused for all remaining characters in SET1. Use the -t option to truncate SET1 to the length of SET2 instead.
Conclusion
tr is a simple but effective command for translating, deleting, and squeezing characters from a stream. For more complex pattern matching and string manipulation, use sed
or awk
. To sort translated output, combine tr with sort
.
If you have any questions, feel free to leave a comment below.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page