When to use tr
Use tr for character translation, deletion and squeezing in a stream. It operates on character sets rather than whole words or regular-expression replacements, so choose another tool for structured substitutions.
Syntax
tr [PARAMETERS] SET1 [SET2]Square brackets mark optional arguments; an ellipsis means repetition. Replace example file names, user names and server addresses with your own values.
Common options and arguments
Usage examples
Convert Latin to upper case
printf '%s\n' 'hello' | tr '[:lower:]' '[:upper:]'Lowercase characters are translated to uppercase according to the character classes and locale.
Remove carriage returns
tr -d '\r' < ./windows.txt > ./unix.txtCarriage returns are removed into a separate file, useful for simple CRLF text conversion.
Compress duplicate spaces
tr -s ' ' < ./input.txtRepeated ordinary spaces are collapsed to one; other whitespace is not covered by that literal set.
Notes and limitations
- tr reads standard input and operates on character sets rather than words.
- Multibyte text support is implementation and locale dependent; For complex Unicode transformations, another tool is needed.
Common questions and troubleshooting
Can tr replace one word with another?
It translates individual characters rather than matching words. Use sed or another text processor for string replacement.
Is deleting every carriage return always safe?
Only when those bytes are unwanted. Embedded carriage returns may carry meaning in binary or unusual text formats; inspect the input first.
Further help
Options vary by version and distribution. Check the installed documentation with this command: man tr.