| title | Linux Command Tutorial: tr | ||||
|---|---|---|---|---|---|
| date | 2026-09-12 00:00:00 +0000 | ||||
| categories |
|
||||
| tags |
|
||||
| draft | false | ||||
| slug | linux-tr-tutorial | ||||
| description | Authoritative reference tutorial for tr (GNU Coreutils), detailing character translation, deletion (-d), squeezing (-s), character classes, and POSIX portability. | ||||
| upstream_suite | gnu-coreutils | ||||
| upstream_version | GNU Coreutils 9.11 | ||||
| posix_standard | POSIX.1-2024 | ||||
| research_date | 2026-09-12 |
The Linux Command Tutorial series provides rigorous, upstream-verified references for essential system commands across Linux distributions and UNIX-like environments. Each article focuses on a single executable, combining exhaustive option documentation, verified real-world examples, security boundaries, and best practices directly derived from official source documentation and POSIX standards.
Upstream: GNU Coreutils 9.11 | POSIX: POSIX.1-2024 (with GNU extensions) | Safety Tier: safe-read-only | Scope: text-processing
tr (translate) translates, squeezes, or deletes characters from standard input, writing the results to standard output. It operates strictly on character streams (not file path arguments) as a fast, byte-level transformation filter.
- Upstream Project & Provenance: Distributed in GNU Coreutils (
coreutils). - Portability & Standards Baseline: Standardized in IEEE Std 1003.1-2024 (POSIX.1-2024).
- Target Research Implementation: Audited against GNU Coreutils 9.11 (
tr(1)). - Applicability & Lifecycle: The standard filter for case conversion, stripping carriage returns, and collapsing whitespace.
tr [OPTION]... SET1 [SET2]trreads strictly from standard input (stdin). It does not take input file path operands.- Translation (
SET1 -> SET2): Maps the N-th character inSET1to the N-th character inSET2. IfSET2is shorter thanSET1,SET2is padded to match the length ofSET1with its last character.
| Short Flag | Long Flag | Description | POSIX Defined |
|---|---|---|---|
-c, -C |
--complement |
Use the complement of SET1. | Yes |
-d |
--delete |
Delete characters in SET1, do not translate. | Yes |
-s |
--squeeze-repeats |
Replace each sequence of a repeated character with a single occurrence. | Yes |
-t |
--truncate-set1 |
First truncate SET1 to length of SET2. | Yes |
POSIX character classes are specified enclosed in [: and :]:
[:alnum:],[:alpha:],[:digit:],[:lower:],[:upper:],[:space:],[:punct:],[:cntrl:].
| Operation | Command | Notes |
|---|---|---|
| Lowercase to uppercase | tr '[:lower:]' '[:upper:]' < file.txt |
Standard POSIX case translation |
| Uppercase to lowercase | tr '[:upper:]' '[:lower:]' < file.txt |
Standard POSIX lowercase fold |
| Delete specific characters | tr -d '\r' < dos.txt > unix.txt |
Strips DOS carriage return bytes |
| Squeeze repeated spaces | tr -s ' ' < spaced.txt |
Collapses consecutive spaces into one |
| Replace character with newline | tr ' ' '\n' < words.txt |
Splits space-separated list into lines |
| Keep only alphanumerics | tr -cd '[:alnum:]' < input.txt |
Deletes complement of alphanumeric set |
| Translate delimiters | tr ':' '\t' < /etc/passwd |
Converts colons to tabs |
echo "linux command tutorial" | tr '[:lower:]' '[:upper:]'LINUX COMMAND TUTORIAL
echo "Phone: (555) 123-4567" | tr -d ' ()-'Phone:5551234567
Cleaning up a DOS text file in a pipeline:
cat windows_file.txt | tr -d '\r' > unix_file.txt- Deletes every
\rcharacter without altering line feeds (\n).
Normalizing inconsistent multi-space formatting into a single space:
echo "column1 column2 column3" | tr -s ' 'column1 column2 column3
Purging all non-alphanumeric characters, replacing them with newlines to generate a word frequency list:
tr -c '[:alnum:]' '\n' < article.txt | tr '[:upper:]' '[:lower:]' | sort | uniq -c | sort -nr | head -n 4- Uses
-cto target everything that is not alphanumeric, mapping it to newlines.
Extracting printable characters from /dev/urandom:
tr -dc 'A-Za-z0-9!@#$%^&*' < /dev/urandom | head -c 24; echo7b@Wq!9mK#1vL8zP$4xN&2aQ
- Technical Analysis:
-d(delete) combined with-c(complement) strips all bytes except those in the specified character set, producing a clean random token stream.
| Exit Code | Meaning |
|---|---|
0 |
Success: character translation completed. |
>0 |
An error occurred (invalid character range, missing SET operand). |
Important
tr does not accept file paths as arguments. Running tr 'a' 'b' file.txt treats "file.txt" as part of SET2, causing catastrophic translation errors or infinite hangs waiting on stdin. Always supply input via shell redirection (< file.txt) or a pipeline (cat file.txt | tr ...).
- Always Use POSIX Classes Over Ranges for Case Folding:
- Guidance: Use
tr '[:lower:]' '[:upper:]'instead oftr 'a-z' 'A-Z'. - Authoritative Justification: POSIX documentation notes that character classes properly respect locale-specific casing rules.
- Guidance: Use
- Use
tr -d '\r'for Fast DOS to UNIX Conversion:- Guidance: Strip
\rusingtr -d '\r'rather than installingdos2unix. - Authoritative Justification: Standardized in all UNIX systems and operates directly on standard streams.
- Guidance: Strip
- Use
-sBefore Line Tokenization:- Guidance: Squeeze repeated delimiters (
tr -s ' ') before piping intocut -d' '. - Authoritative Justification: Prevents empty field proliferation caused by consecutive spaces.
- Guidance: Squeeze repeated delimiters (
- GNU Coreutils tr Manual: https://www.gnu.org/software/coreutils/manual/html_node/tr-invocation.html
- POSIX.1-2024 tr Specification: The Open Group Base Specifications Issue 8. https://pubs.opengroup.org/onlinepubs/9799919799/utilities/tr.html