trTranslate, squeeze or delete characters
tr replaces characters with other characters, squeezes repeats (-s) or deletes them (-d). It only reads stdin. Typical jobs: split a list onto lines, upper-case a name, strip the Windows \r that breaks a script, or turn the NUL-separated /proc/PID/environ into readable lines.
tr [OPTION]... SET1 [SET2] (input from stdin only)Turn a comma list into lines, read your PATH one entry per line, and build an env var prefix from a service name.
trCharacter translator. It works on single characters, never on words or patterns.
tr builds a 256-entry translation table from the two sets before reading any input, then streams stdin to stdout byte by byte, looking each byte up in the table. Ranges like a-z and classes like [:digit:] or [:space:] expand into lists of characters; when SET2 is shorter than SET1, GNU tr repeats its last character to fill it. -d deletes every byte in SET1, -c complements SET1 (everything NOT in it), and -s squeezes repeated output characters. Because it works on bytes, GNU tr does not handle multi-byte UTF-8 characters: tr can't upper-case é. It also has no idea of words or strings: tr 'abc' 'xyz' maps a to x, b to y and c to z everywhere, which surprises people who expect sed-like replacement. The upside is speed and zero memory: it's the right tool for byte-level cleanup like removing \r or NUL bytes.
SET1 SET2Map each character in SET1 to the one at the same position in SET2-d SETDelete all characters in SET-s SETSqueeze runs of a repeated character into one-c / -CComplement SET1: act on every character NOT in ita-z, 0-9Ranges of characters[:upper:] [:lower:]Classes; also [:digit:] [:alpha:] [:alnum:] [:space:] [:punct:]\n \r \t \0Newline, carriage return, tab, NUL bytetr 'http' 'https' < urls.txttr maps characters one to one; it can't insert or replace strings. Here h->h, t->t, t->t, p->p: nothing changes.
sed 's|http://|https://|' urls.txttr -d '\r' < script.sh > script.shThe shell truncates script.sh before tr reads it, leaving an empty file.
tr -d '\r' < script.sh > script.tmp && mv script.tmp script.sh # or: dos2unix script.shtr [a-z] [A-Z] without quotesUnquoted brackets are a shell glob: if a file named a or A exists in the directory, the shell replaces the pattern with it.
tr '[:lower:]' '[:upper:]' # or: tr a-z A-Zecho 'café' | tr '[:lower:]' '[:upper:]' expecting CAFÉGNU tr works on bytes, so multi-byte UTF-8 letters are not converted.
echo 'café' | sed 's/.*/\U&/' # or awk '{print toupper($0)}' with gawkTry these on a Linux machine of your own. Each task has a hint and a solution.
Print each directory in your PATH on its own line.
Print ~/app/config/app.env with every letter in upper case.
Collapse the runs of spaces in the output of df -h so that each column is separated by exactly one space.
Show the environment of your current shell process from /proc, one variable per line.
trTranslate, squeeze or delete characters
tr replaces characters with other characters, squeezes repeats (-s) or deletes them (-d). It only reads stdin. Typical jobs: split a list onto lines, upper-case a name, strip the Windows \r that breaks a script, or turn the NUL-separated /proc/PID/environ into readable lines.
tr [OPTION]... SET1 [SET2] (input from stdin only)Turn a comma list into lines, read your PATH one entry per line, and build an env var prefix from a service name.
trCharacter translator. It works on single characters, never on words or patterns.
tr builds a 256-entry translation table from the two sets before reading any input, then streams stdin to stdout byte by byte, looking each byte up in the table. Ranges like a-z and classes like [:digit:] or [:space:] expand into lists of characters; when SET2 is shorter than SET1, GNU tr repeats its last character to fill it. -d deletes every byte in SET1, -c complements SET1 (everything NOT in it), and -s squeezes repeated output characters. Because it works on bytes, GNU tr does not handle multi-byte UTF-8 characters: tr can't upper-case é. It also has no idea of words or strings: tr 'abc' 'xyz' maps a to x, b to y and c to z everywhere, which surprises people who expect sed-like replacement. The upside is speed and zero memory: it's the right tool for byte-level cleanup like removing \r or NUL bytes.
SET1 SET2Map each character in SET1 to the one at the same position in SET2-d SETDelete all characters in SET-s SETSqueeze runs of a repeated character into one-c / -CComplement SET1: act on every character NOT in ita-z, 0-9Ranges of characters[:upper:] [:lower:]Classes; also [:digit:] [:alpha:] [:alnum:] [:space:] [:punct:]\n \r \t \0Newline, carriage return, tab, NUL bytetr 'http' 'https' < urls.txttr maps characters one to one; it can't insert or replace strings. Here h->h, t->t, t->t, p->p: nothing changes.
sed 's|http://|https://|' urls.txttr -d '\r' < script.sh > script.shThe shell truncates script.sh before tr reads it, leaving an empty file.
tr -d '\r' < script.sh > script.tmp && mv script.tmp script.sh # or: dos2unix script.shtr [a-z] [A-Z] without quotesUnquoted brackets are a shell glob: if a file named a or A exists in the directory, the shell replaces the pattern with it.
tr '[:lower:]' '[:upper:]' # or: tr a-z A-Zecho 'café' | tr '[:lower:]' '[:upper:]' expecting CAFÉGNU tr works on bytes, so multi-byte UTF-8 letters are not converted.
echo 'café' | sed 's/.*/\U&/' # or awk '{print toupper($0)}' with gawkTry these on a Linux machine of your own. Each task has a hint and a solution.
Print each directory in your PATH on its own line.
Print ~/app/config/app.env with every letter in upper case.
Collapse the runs of spaces in the output of df -h so that each column is separated by exactly one space.
Show the environment of your current shell process from /proc, one variable per line.