Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

How to Use the tr Command for Character Translation and Deletion in Linux

Published

Linux

On Linux and other Unix-like operating systems, text manipulation is a daily necessity for users, system administrators, and developers. Among the many utilities available, the tr command stands out as a simple yet powerful tool for transforming streams of text. The name tr stands for “translate” or “transliterate,” and its primary function is to substitute, delete, or squeeze characters from its input.

Unlike some other text-processing tools (awk, sed, or cut), tr is focused solely on character-level operations. It is most commonly used in pipelines, where it reads from standard input and writes to standard output, making it ideal for batch jobs, scripting, and one-off command-line tasks.

Whether you want to change lowercase letters to uppercase, remove spaces, eliminate punctuation, or compress sequences of the same character, tr can do it efficiently and with minimal syntax.

This article offers a thorough look at everything you need to know about the tr command, from basic usage to advanced techniques, with plenty of real-world examples.

What is the tr Command?

The tr command processes input text, transforming it according to the rules and character sets you specify. It can:

  • Translate (replace) specified characters with others.
  • Delete characters from the input.
  • Squeeze (collapse) sequences of repeated characters into a single character.

tr always reads from standard input (stdin) and writes to standard output (stdout). It does not work directly on files (you must use shell redirection or piping).

General syntax: tr [OPTIONS] SET1 [SET2]

  • SET1: Defines the characters to be replaced, deleted, or squeezed.
  • SET2: Defines the replacement characters for translation operations.

Basic Usage: Translating Characters

1. Convert Lowercase to Uppercase

To translate all lowercase letters to uppercase in a string:

echo "hello linux world" | tr 'a-z' 'A-Z'

Output:

HELLO LINUX WORLD

2. Convert Uppercase to Lowercase

echo "WELCOME TO LINUX" | tr 'A-Z' 'a-z'

Output:

welcome to linux

3. Replace Spaces with Underscores

echo "file name with spaces" | tr ' ' '_'

Output:

file_name_with_spaces

4. Replace Multiple Characters

You can swap multiple character pairs at once:

echo "abc123" | tr 'abc' 'xyz'

Output:

xyz123

This replaces every “a” with “x”, “b” with “y”, and “c” with “z”.

Deleting Characters

The -d flag deletes all occurrences of the specified characters.

1. Remove Digits from a String

echo "abc123def456" | tr -d '0-9'

Output:

abcdef

2. Remove Vowels

echo "This is a test" | tr -d 'aeiouAEIOU'

Output:

Ths s  tst

3. Remove Newlines (Flatten Text)

To convert a file into a single line by removing newlines:

tr -d '\n' < input.txt > output.txt

Squeezing Repeated Characters

The -s (squeeze) option replaces each sequence of a repeated character with a single instance.

1. Squeeze Multiple Spaces

echo "This   is     a  test" | tr -s ' '

Output:

This is a test

2. Squeeze Newlines

Collapses multiple blank lines into single newlines:

tr -s '\n' < input.txt > output.txt

Deleting All Except a Character Set (Complement)

The -c (complement) option inverts the character set. Combined with -d, you can remove everything except the characters you specify.

1. Keep Only Digits

echo "user123data456" | tr -cd '0-9'

Output:

123456

2. Keep Only Letters

echo "abc123DEF!@#" | tr -cd 'a-zA-Z'

Output:

abcDEF

Using Character Classes and Ranges

tr understands character ranges and POSIX character classes.

1. Ranges

  • a-z (all lowercase letters)
  • A-Z (all uppercase letters)
  • 0-9 (all digits)

2. POSIX Classes

  • [:lower:] (all lowercase letters)
  • [:upper:] (all uppercase letters)
  • [:digit:] (all digits)
  • [:space:] (all whitespace characters)

Example: Remove all digits using a class:

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

Output:

abcdef

Advanced Examples

1. Replace Tabs with Commas

tr '\t' ',' < file.txt

2. Number Lines and Replace Tabs in Output

Combine with nl to number lines and format output:

nl file.txt | tr '\t' ': '

3. Prepare a List: Convert Spaces to Newlines

echo "one two three" | tr ' ' '\n'

Output:

one
two
three

Using tr in Scripts and Pipelines

The tr command is often used in shell scripts and command pipelines for fast, in-place transformations.

Example: Clean and Parse a CSV

Suppose you have a comma-separated list and want to remove all spaces:

echo "a, b, c, d" | tr -d ' '

Output:

a,b,c,d

Example: Normalize a File

To make all characters in a file lowercase and remove punctuation:

cat file.txt | tr 'A-Z' 'a-z' | tr -d '[:punct:]'

Limitations of the tr Command

  • tr works only on standard input and output. It does not edit files in place.
  • It only supports single-character operations (not multi-character strings or patterns).
  • For complex substitutions or pattern matching, use sed or awk.

Tips and Best Practices

  • Always use single quotes around character sets to prevent shell expansion.
  • For large files or repetitive tasks, combine tr with other utilities like sort, uniq, cut, or awk.
  • Test your tr command on small samples before running on large or important data.

Summary Table: Common tr Use Cases

Task Command Example Output Example
Convert lowercase to uppercase tr 'a-z' 'A-Z' hello → HELLO
Delete digits tr -d '0-9' abc123 → abc
Squeeze spaces tr -s ' ' This is a test → This is a test
Replace spaces with underscores tr ' ' '_' file name → file_name
Keep only lowercase letters tr -cd 'a-z' AbC123 → bc
Replace tabs with commas tr '\t' ',' a b c → a,b,c

Conclusion

The tr command is a fundamental utility in Linux for anyone who works with text data. Its simplicity, speed, and power make it a staple in many daily workflows. With tr, you can easily perform character replacements, deletions, and compressions—often with a single, concise command.

Next time you need to clean up a file, convert text case, or remove unwanted characters in a pipeline, reach for tr. As you practice, you’ll discover it’s one of the best tools for quick, reliable text manipulation on the command line.

Related guides

Account services are unavailable until the production backend is configured.