tr command in Unix/Linux with examples
The tr (translate) command in Linux is used to transform or delete specific characters from text input. It is commonly used for tasks like changing letter cases, removing unwanted characters, or compressing repeating characters.
- Reads input from standard input (stdin) or a file and outputs modified text.
- Can convert uppercase to lowercase or vice versa.
- Supports character replacement and range operations (e.g., [a-z], [A-Z]).
Example
Suppose we have a file named test.txt that contains both lowercase and uppercase text. We can use the tr command to convert all lowercase characters to uppercase.
cat test.txtOutput:

- To convert all lowercase letters to uppercase, use either of the following:
Syntax
It is often used together with pipes (|) or redirection (>>) to process the content of files in more complex ways.
tr [OPTION] SET1 [SET2]
Options
-c: Applies the operation to characters not in the specified set (complements the set).-d: Deletes all characters in the first set from the output.-s: Replaces repeated occurrences of characters in set1 with a single occurrence.-t: Truncates set1 to match the length of set2 during translation.
Example 1: Using character ranges
cat test.txt | tr [a-z] [A-Z]Output:

Example 2: Using predefined character classes
cat test.txt | tr [:lower:] [:upper:]Output:
WELCOME TO
GEEKSFORGEEKSExample 3: Using input redirection
tr [:lower:] [:upper:] < test.txtOutput:
WELCOME TO
GEEKSFORGEEKS[a-z] [A-Z]converts lowercase letters to uppercase.[:lower:] [:upper:]does the same but is more portable across systems.- Input redirection < allows tr to read directly from a file without using cat.
tr Command Options
Here are the commonly used options in the tr command in Linux, along with examples and short explanations
Example 1. How to translate white-space characters to tabs
The following command translates all the white-space characters to tabs:
echo "Welcome To GeeksforGeeks" | tr [:space:] "\t"

Output:
Welcome To GeeksforGeeksIn the previous example we can also use redirection to provide intput for tr. Although this time we will use a here string for that:
tr [:space:] "\t" <<< "Welcome To GeeksforGeeks"
Output:
Welcome To GeeksforGeeksExample 2. How to translate braces into parenthesis
You can also translate from and to a file. In this example we will translate braces in a file with parenthesis.
$ cat greekfileOutput:
{WELCOME TO}
GeeksforGeeks$ tr "{}" "()" <greekfile >newfile.txtOutput:
(WELCOME TO)
GeeksforGeeksThe above command will read each character from “geekfile.txt”, translate if it is a brace, and write the output to “newfile.txt”.
Example 3. How to squeeze a sequence of repetitive characters using -s option
To squeeze repetitive occurrences of characters specified in a set use the -s option. This removes repeated instances of characters of the last SET specified. OR we can say that, you can convert multiple continuous spaces with a single space:
$ echo "Welcome To GeeksforGeeks" | tr -s " "
Output:
Welcome To GeeksforGeeksAnd again, accomplish the same task but using a string here:
tr -s " " <<< "Welcome To GeeksforGeeks"
Output:
Welcome To GeeksforGeeksExample 4. How to delete specified characters using -d option
To delete specific characters use the -d option. This option deletes characters in the first set specified.
echo "Welcome To GeeksforGeeks" | tr -d W
Output:
elcome To GeeksforGeeksOr equivalently use:
tr -d W <<< "Welcome to GeeksforGeeks"Output:
elcome To GeeksforGeeksExample 5. To remove all the digits from the string
You can use:
echo "my ID is 73535" | tr -d [:digit:]
or
tr -d [:digit:] <<< "my ID is 73535"Example 6. How to complement the sets using -c option
You can complement the SET1 using -c option. For example, to remove all characters except digits, you can use the following.
$ echo "my ID is 73535" | tr -cd [:digit:]
or
$ tr -cd [:digit:] <<< "my ID is 73535"
Output:
73535&list=PLqM7alHXFySFc4KtwEZTANgmyJm3NqS_L&ab_channel=GeeksforGeeks