Linux tr Command: Translate and Delete Characters

tr replaces or deletes characters from standard input and compresses repeated characters. First, remember that it does not take file names as arguments directly.

What is the tr command?

tr is a character-level filter. To find string patterns and replace them contextually, sed is more suitable. Since the interpretation of character ranges can be affected by locale, check the input character set in scripts.

Basic syntax

tr [OPTION] SET1 [SET2] < INPUTFILE

Installed implementations and options may vary depending on the distribution. Use man tr to check the description on your current system.

Examples

Lowercase to uppercase

Pass input through a pipe and display the results on the screen.

printf '%s\n' hello | tr '[:lower:]' '[:upper:]'

Delete specific characters

-d removes characters that belong to the specified character set.

tr -d '\r' < windows-lines.txt

Repeat Space Compression

Reduces consecutive space characters to one. A separate substitution is required to unify tabs and spaces into one type.

tr -s ' ' < notes.txt

Character-level conversion result

tr replaces or deletes characters from standard input one by one. The following is an example of changing uppercase English letters to lowercase.

printf 'HELLO 123\n' | tr '[:upper:]' '[:lower:]'

Example output:

hello 123

Numbers and spaces are not subject to conversion, so they remain unchanged. Since tr does not take a file path argument, when processing files, use redirection like tr... < original > result. For complex replacements of multi-byte characters or word/regex replacements, sed or other text processing tools are more suitable.

Main Options and Format

Options/Format Description
-d Deletes characters corresponding to set1.
-s Compresses repeated occurrences of the corresponding character to one.
-c Selects characters that are not in set1.
[:lower:] / [:upper:] Represents uppercase and lowercase sets with a character class.

Precautions when using

Does not modify the input file. tr ... < file > file may cause the shell to empty the file first, so use a separate output file. The sets in tr are not full regular expressions, and check implementation and locale limitations for UTF-8 multibyte handling.

Frequently Asked Questions

Can you put a file name like tr file.txt?

No. The content of the file must be provided to standard input like tr... < file.txt.

Official Documentation

The exact behavior of the options and implementation differences can be found in the official tr documentation.

More in This Category
Linux top Command: Monitor Processes in Real Time

Linux top Command: Monitor Processes in Real Time

Learn how to monitor Processes in Real Time with the Linux top command, including practical examples, key options, and important precautions.

Linux head Command: Display the Beginning of a File

Linux head Command: Display the Beginning of a File

Learn how Linux head prints the first lines or bytes of files and pipelines, labels multiple inputs, excludes a trailing portion, and handles multibyte data.

Linux Tutorial / What Is Linux? Understanding the Kernel, Distributions, and Open Source

Linux Tutorial / What Is Linux? Understanding the Kernel, Distributions, and Open Source

Understand how the Linux kernel differs from a distribution, what user space and open source mean, and how to check your system's kernel and distribution versions.

Linux du Command: Check File and Directory Disk Usage

Linux du Command: Check File and Directory Disk Usage

Learn how to use the Linux du command to measure and summarize disk space used by files and directories, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux mkdir Command: Create Directories and Parent Paths

Linux mkdir Command: Create Directories and Parent Paths

Learn how to create directories with Linux mkdir, build missing parent paths, set permissions, understand umask behavior, and verify results in scripts.

Linux ss Command: Inspect Open Ports and Sockets

Linux ss Command: Inspect Open Ports and Sockets

Learn how to inspect Open Ports and Sockets with the Linux ss command, including practical examples, key options, and important precautions.

Linux bg Command: Resume a Shell Job in the Background

Linux bg Command: Resume a Shell Job in the Background

Learn how to resume a Shell Job in the Background with the Linux bg command, including practical examples, key options, and important precautions.

Linux screen Command: Create and Manage Detachable Terminal Sessions

Linux screen Command: Create and Manage Detachable Terminal Sessions

Learn how to use the Linux screen command to keep terminal sessions running and reconnect to them later, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux curl Command: Make URL Requests and Inspect HTTP Responses

Linux curl Command: Make URL Requests and Inspect HTTP Responses

Learn how to make URL Requests and Inspect HTTP Responses with the Linux curl command, including practical examples, key options, and important precautions.

Linux clear Command: Clear the Terminal Display

Linux clear Command: Clear the Terminal Display

Learn how to use the Linux clear command to clear the visible terminal screen and understand scrollback behavior, with essential options, practical examples, output interpretation, and common troubleshooting tips.