Tr Command in Linux with Examples

Tr Command in Linux

The Linux operating system is renowned for its powerful command-line interface, offering a plethora of commands that enable users to perform a wide range of tasks. One such command is the tr command, a versatile tool used for translating or deleting characters. This command is particularly useful for text processing tasks, such as converting cases, deleting specific characters, or replacing one set of characters with another. This article provides a comprehensive guide on the tr command, its syntax, options, and practical examples of its usage.


Before delving into the tr command, it’s essential to have a basic understanding of the Linux operating system and its command-line interface. You should have a system running Linux and access to the terminal. Familiarity with basic Linux commands and text-processing concepts would be beneficial but not mandatory.

Syntax and Options

The tr command follows a simple syntax:

tr [OPTION]... SET1 [SET2]

Here, SET1 and SET2 are sets of characters. The tr command reads text from the standard input, replaces characters in SET1 with corresponding characters in SET2, and writes the result to the standard output. The tr command offers several options:

  • -c or -C: Complements the set of characters in SET1.
  • -d: Deletes characters in SET1.
  • -s: Squeezes repeated output characters of SET2 into one character.
  • -t: Truncates SET1 to the length of SET2.

Examples of tr Command in Linux

Case Conversion

The tr command can convert text from lower case to upper case and vice versa. For instance, to convert the text “Hello World” to lower case, you would use:

echo "Hello World" | tr '[:upper:]' '[:lower:]'

This command will output “hello world”.

Deleting Specific Characters

The tr command can also delete specific characters from the input. For example, to remove all digits from a text, you can use:

echo "Hello123" | tr -d '[:digit:]'

Squeezing Repeating Characters

The tr command can replace repeated characters with a single occurrence. For instance, to squeeze repeated spaces in a text, you can use:

echo "Hello World" | tr -s ' '

This command will output “Hello World”.

Basic Text Replacement

The tr command can replace one set of characters with another. For example, to replace all occurrences of ‘l’ with ‘w’ in a text, you can use:

echo "Hello World" | tr 'l' 'w'

This command will output “Hewwo Worwd”.

Using tr with Pipes and Redirects

The tr command can be used in conjunction with pipes (|) and redirects (> and <) for more complex file content processing. For example, to convert the contents of a file to upper case and save the result in another file, you can use:

cat input.txt | tr '[:lower:]' '[:upper:]' > output.txt

Common Issues and Solutions

While using the tr command, you might encounter some common issues. For instance, if you try to delete or replace a character that does not exist in the input, the tr command will not return an error; it will simply do nothing. To avoid this, always ensure that the characters you want to delete or replace exist in the input.

Another common issue is misunderstanding the use of SET1 and SET2. Remember, the tr command replaces each character in SET1 with the corresponding character in SET2. If SET2 is shorter than SET1, the last character of SET2 is duplicated enough times to match the length of SET1.

Best Practices

When using the tr command, keep the following best practices in mind:

  • Always double-check your command before executing it, especially when working with important files.
  • Use the -c option wisely. It complements the set of characters, which means it can affect more characters than you might initially think.
  • Remember that the tr command works with bytes, not with characters. This means it might not work as expected with multibyte characters, like those in UTF-8 encoded files.


The tr command is a powerful tool in the Linux command-line interface, offering a wide range of text processing capabilities. Whether you’re converting cases, deleting characters, or replacing one set of characters with another, the tr command has you covered. With its simple syntax and versatile options, it’s a command that every Linux user should master. So, why wait? Open your terminal and start practicing with the tr command today!


r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button