tr: translate, squeeze and delete characters in a stream

Use tr for case conversion, collapsing repeated characters and stripping characters like carriage returns on Linux.

LinuxtrtextencodingLow risk
Pipe text through tr 'a-z' 'A-Z' to uppercase, tr -s ' ' to squeeze repeated spaces, or tr -d '\r' to delete characters.

01Command

Command
$ tr '[:upper:]' '[:lower:]' < input.txt && tr -s ' ' < data.txt && tr -d '\r' < win.txt

02What it does

tr works purely on single characters: SET1 maps onto SET2 position by position, so [:upper:] to [:lower:] does locale-safe case conversion and A-Z ranges do the quick version. -d deletes listed characters outright, and -s squeezes runs of the same character into one — perfect for cleaning up spaced-out log output. Because it only reads stdin, tr is always used inside a pipe or with redirection.

03Risk

RiskLow risk
!Warning: tr cannot work on strings or files by name — it never takes a filename argument, only stdin; multi-character replacement needs sed.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

  • tr(1P) — POSIX manual page
    man7.org · technical · 2026-09-17
    Evidence: POSIX defines character-class translation, -d and -s for tr
  • tr(1) — Arch Linux manual
    Arch Linux · technical · 2026-09-17
    Evidence: Documents SET1/SET2 translation, -d delete, -s squeeze repeats, and [:upper:]/[:lower:] classes

Last verified: 2026-09-17 · Category: Files & Folders

06Related solutions