Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

tr

Replace, delete or squeeze single characters in a text stream.

Level
Everyday Linux
Group
Text and search
Needs root
No
Package
coreutils

What it does

tr (translate) works on characters, not words. It reads standard input only (no file names), so you use it in a pipe or with <.

Typical jobs: change lower case to upper case, change one separator to another, remove characters (like Windows \r), or squeeze repeated spaces into one.

How to write it

tr [OPTION]... SET1 [SET2]

Options 12

Most used

-d, --delete
Delete the characters in SET1.
-s, --squeeze-repeats
Replace a run of the same character (from the last set) with one.
-c, -C, --complement
Use every character that is NOT in SET1.
-t, --truncate-set1
Make SET1 as long as SET2 first.
--help
Show help.
--version
Show the version.

Sets

a-z, A-Z, 0-9
Ranges of characters.
[:lower:] [:upper:]
Lower case / upper case letters.
[:digit:] [:alpha:] [:alnum:]
Digits / letters / letters and digits.
[:space:] [:blank:]
White space / spaces and tabs.
[:punct:] [:cntrl:] [:print:]
Punctuation / control characters / printable characters.
\n \t \r \\
New line, tab, carriage return, backslash.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Lower case to upper case

    echo "server1" | tr 'a-z' 'A-Z'
    SERVER1

    Same as tr '[:lower:]' '[:upper:]'.

  2. 2Remove Windows line endings

    tr -d '\r' < script-from-windows.sh > script.sh

    Fixes "bad interpreter: No such file or directory".

  3. 3Squeeze many spaces into one

    ps aux | tr -s ' ' | cut -d' ' -f2,11

    After squeezing, cut can use the space as separator.

  4. 4The PATH one folder per line

    echo "$PATH" | tr ':' '\n'

    Easy to read.

  5. 5Remove all digits

    echo "abc123def" | tr -d '0-9'
    abcdef

    Delete a set of characters.

  6. 6Keep only letters and digits

    echo "us3r_n@me!" | tr -cd '[:alnum:]'
    us3rnme

    -c = everything else, -d = delete it.

  7. 7Make a random password

    tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 20; echo

    20 random letters and digits.

  8. 8Split words into lines

    echo "one two three" | tr ' ' '\n'

    Then sort | uniq -c can count words.

  9. 9Convert a CSV to TAB separated

    tr ',' '\t' < data.csv > data.tsv

    Simple files only (no quoted commas).

  10. 10Change a hostname to a valid label

    echo "Web Server 01" | tr '[:upper:] ' '[:lower:]-'
    web-server-01

    Spaces become dashes, letters become lower case.

  11. 11Read NUL-separated data

    tr '\0' '\n' < /proc/1/cmdline

    /proc files separate words with NUL bytes.

Watch out

  • tr does not read file names: use tr … < file.
  • tr changes single characters, not words. For words use sed.

Tips to remember

  • tr -d delete, tr -s squeeze, tr 'a-z' 'A-Z' upper case.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man tr