
What is the Linux tr command?
The tr command in Linux is an excellent tool which can be used for text manipulation of specific characters within text files.
With this tool you are able to translate strings of characters from standard input (also known as stdin) and it will write that translation into standard output (also known as stdout).
As an example the tr command can transform or convert characters from upper to lower case (basic text replacement), delete specific characters or even “find and replace” specific characters strings.
Within this article, we’ll cover the basics of the tr command in Linux as well as some use cases to help you get started utilizing the command.
- What is the Linux tr command?
- Basic syntax for the Linux tr command
- Linux tr command flags/options
- Replacing characters with tr
- Change character case for text using the Linux tr command
- Delete characters using the tr command
- Deleting numbers from a text file using tr
- Convert white-spaces to tabs for spacing using the tr command
- How to remove white space in a file using tr
- How to remove newline characters from a file
- How to use find and replace with the tr command
- Squeezing repeating characters with tr
- Conclusion
Basic syntax for the Linux tr command
The general syntax for the Linux tr command is as follows:
tr [options/flags] $set1 $set2
From the above we have two sets where set1 is our first set (a specified set or string of characters to search for) and we would replace it with what we enter in for our second set (set2).
Linux tr command flags/options
Use the following to see all options available or refer to the man pages for the Linux tr command:
# tr --help

Replacing characters with tr
For example, in the following command, if we wanted to replace certain characters within a string of text such as swapping out all lowercase m’s with uppercase M’s it would be done as follows:
$ echo "metallica rocks" | tr m M

As we can see from the above we piped tr into an echo command for a string of text and our first string (m) is being searched for and then replaced by the second string (M).
With that being said we have replaced all occurrences of the lowercase m within that string of text to an uppercase M.
To expand on that, here is another example of the syntax to help it make more sense:
echo "Sample String of Text" | tr $string-to-replace $string-to-use-as-replacement
Change character case for text using the Linux tr command
We can change lowercase characters and uppercase characters with ease using the Linux tr command such as in the following example:
cat sample-file.txt | tr [:upper:] [:lower:]

Based off the previous example, alternatively we could also change text from lowercase to uppercase

All we did here was output the results from our first command into a new file so it was all lowercase. Then we reversed the sequence of the tr command for [:lower:] and [:upper:].
On the flip side, you can accomplish the same using the following to swap upper to lower case:
$ cat example.txt | tr [A-Z] [a-z]

And to change from lower to uppercase we would just re-position the sets as follows:
$ cat example.txt | tr [a-z] [A-Z]
Delete characters using the tr command
With the tr command we can also delete certain characters within text such as in the following example:

In the above we see the example2.txt file contains “example text” so we’ve utilized the -d flag to delete instances of any ‘e’ characters.
Deleting numbers from a text file using tr
Such as above where we removed a specific letter from a string of text we can do the same but with numbers.
Lets say our file contains the following information:

If we’d like to remove all numbers within the file it could be done with the following:
$ cat example.txt | tr -d '0-9'

We’re utilizing the -d to delete and specifying numbers 0-9 to be removed and as we can see it has filtered those out when piping the above tr command with our cat command.
You could also be more specific and only include specific numbers in the set such as the following if we wants to remove just the numbers 5 and 6:
$ cat example.txt | tr -d '5-6'
Convert white-spaces to tabs for spacing using the tr command
Let’s say we need to adjust spacing between text within a file.
This can be useful if working with certain types of coding languages such as YAML, where spacing is incredibly crucial to avoid syntax issues.
As we see below each word is only separated in single spaces:

With the following we could convert those to be spaced in tabs instead.
So lets cat the original file and use the following to write output to a new file:
$ cat example.txt | tr [:space:] '\t' > new-file.txt

Now we open the new file and see how each word is spaced with tabs to separate them instead of normal single spaces.
This type of use case could be helpful in situations where spacing is important for proper syntax:

How to remove white space in a file using tr
This is how we would remove white space characters from some random text which we’re echoing out:
$ echo "NameHero Rules" | tr -d ' '

How to remove newline characters from a file
Lets say we have a text file which contains the following newline characters:

Let’s remove the newline characters and put them all together on a single line:
$ cat example.txt | tr -s '\n' ' '

How to use find and replace with the tr command
Using tr we’re able to “find and replace” strings within files as well.
In the following example we’ll show how you can swap out the braces ‘{}’ with parenthesis ‘()’.
Here we have a file with the following contents:

Our goal is to find all instances of braces {} within the file and swap them out with parenthesis ().
This can be done with the following which is a basic find and replace:
$ cat example.txt | tr "{}" "()"

This could be a useful tool if perhaps there was a coding syntax issue within file due to an incorrect symbol being used.
Luckily we can easily correct it by simply using tr to swap out the the incorrect symbol with the correct symbol to resolve the issue.
Squeezing repeating characters with tr
Lets say we have a file with repeating characters and we need to manipulate the text to only leave one instance of a specific character.
For this example lets say our file contains the following:

As we can see there are repeating ‘e’ letters within a few words of text in this file.
To correct that we can use the following to squeeze the repeating characters and clean things up:
$ cat example.txt | tr -s 'e'

Even if we had more than 1 repeated character we can accomplish the same as shown here:

Conclusion
With the tr command in Linux we’re able to accomplish many different types of tasks when it comes to text manipulation.
Such examples we’ve covered could assist in making sure file syntax is correct for spacing or you can also find strings of text and swap them out with the correct string.
Overall the tr command is a useful utility to keep in your tool best as a Linux Administrator and it can be applied to many different situations all to make your job easier.
Leave a Reply