10 Linux tr command practical examples you should know as a system administrator (bookmark this):
The tr command short for translate, is one of the most useful command for manipulating text on the command line.
It allows you to perform useful operations such as converting lowercase characters to uppercase characters, uppercase characters to lowercase characters, character replacing, and deleting characters.
It is usually used in conjunction with other commands via piping.
In this thread, I will show you some of the most examples of the tr command on Linux.
𝟭. 𝗗𝗲𝗹𝗲𝘁𝗶𝗻𝗴 𝗿𝗲𝗽𝗲𝗮𝘁𝗲𝗱 𝗰𝗵𝗮𝗿𝗮𝗰𝘁𝗲𝗿𝘀
You can use the -s option to squeeze a character that is repeating to make it a single character. This option is especially useful when you want to squeeze multiple continuous spaces characters into a single character.
$ echo "Linux is awesome, I'm in love with it." | tr -s " "
From the above example you can clearly see the multiple spaces have been translated to single space.
Instead of using the space character in our character SET for squeezing repeating characters, we can also make use of character classes and still get the same results.
$ echo "Linux is awesome, I'm in love with it." | tr -s "[:space:]"
𝟮. 𝗗𝗲𝗹𝗲𝘁𝗲 𝘀𝗽𝗲𝗰𝗶𝗳𝗶𝗰 𝗰𝗵𝗮𝗿𝗮𝗰𝘁𝗲𝗿𝘀
Using the -d option, you can delete characters you specify. tr command deletes every instance of the "-" character in the following example:
$ echo "Some- people- are- afraid- to- use -the Linux -System." | tr -d "-"
𝟯. 𝗗𝗲𝗹𝗲𝘁𝗲 𝗮𝗹𝗹 𝘁𝗵𝗲 𝗱𝗶𝗴𝗶𝘁𝘀
You can also use the -d option to remove any numbers or digits from your text.
$ echo "Some people are afraid to use the Linux System. 2023" | tr -d "[:digit:]"
Instead of using character classes you can use number character range and still get the same results.
$ echo "Some people are afraid to use the Linux System. 2023" | tr -d "0-9"
𝟰. 𝗖𝗮𝘀𝗲 𝗰𝗼𝗻𝘃𝗲𝗿𝘀𝗶𝗼𝗻
The tr command is frequently used to convert lowercase letters to uppercase letters or the opposite.. The character class [:lower:] matches all lowercase characters, while the character class [:upper:] matches all uppercase characters.
The following will transform characters from upper case to lower case.
$ echo "Some people are afraid to use the Linux System." | tr "[:lower:]" "[:upper:]"
Alternatively, you can also use character range (regular expression) in place of the character classes.
$ echo "Some people are afraid to use the Linux System." | tr "a-z" "A-Z"
𝟱. 𝗥𝗲𝗺𝗼𝘃𝗶𝗻𝗴 𝗻𝗼𝗻 𝗮𝗹𝗽𝗵𝗮𝗻𝘂𝗺𝗲𝗿𝗶𝗰𝗮𝗹 𝗰𝗵𝗮𝗿𝗮𝗰𝘁𝗲𝗿𝘀
We can combine the complement option (-c) with the delete option (-d) to delete all non-alphanumerical characters.
The following command will delete all non-alphanumerical characters.
$ echo "I: have- been@ using# Linux for 12 years." | tr -cd "[:alnum:]"
𝟲. 𝗣𝗿𝗶𝗻𝘁 𝗲𝗮𝗰𝗵 𝘄𝗼𝗿𝗱 𝗼𝗻 𝗮 𝘀𝗶𝗻𝗴𝗹𝗲 𝗹𝗶𝗻𝗲
The $PATH variable is an environmental variable that contains a list of directories separated by a colon that instructs the shell where to look for executable files when you type a command.
So if you're having trouble reading the directories in the $PATH variable, you can make use the tr command to replace the colons with the newline characters so each directory is displayed on a single line.
$ echo $PATH | tr ":" "\ n"
The preceding example will replace all the semicolon in our path variable with the newline characters.
𝟳. 𝗥𝗲𝗺𝗼𝘃𝗲 𝗮𝗹𝗹 𝗻𝗼𝗻-𝗻𝘂𝗺𝗲𝗿𝗶𝗰 𝗰𝗵𝗮𝗿𝗮𝗰𝘁𝗲𝗿𝘀
The -c option instructs tr to use the complement in the SET given. In this example, we want to remove all of the letters and only keep the phone number.
$ echo "Call me at +449883200382" | tr -cd "[:digit:]"
This is very useful if you want to extract phone numbers or employee IDs from text files.
𝟴. 𝗥𝗲𝗺𝗼𝘃𝗲 𝗡𝗲𝘄𝗹𝗶𝗻𝗲 𝗖𝗵𝗮𝗿𝗮𝗰𝘁𝗲𝗿𝘀
Assume you have a text file containing data that looks like this, and you want to remove the newlines and put the words on a single line separated by spaces.
$ cat file.txt
To achieve that, you can redirect you file contents to the tr command as shown on the below command.
$ tr "\ n" " " < file.txt
This will replace each newline character in a text file with a space.
𝟵. 𝗣𝘂𝘁 𝗲𝗮𝗰𝗵 𝘄𝗼𝗿𝗱 𝗶𝗻 𝗮 𝗻𝗲𝘄 𝗹𝗶𝗻𝗲
As a system administrator, you may be given employee names on single lines separated by spaces, and you may want to put each name on a single line for easy readability.
The command below will assist you in accomplishing this by dividing a sentence into multiple lines, with each word on its own line.
$ echo "john james kay fredrick george" | tr "[:space:]" "\ n"
𝟭𝟬. 𝗖𝗼𝗻𝘃𝗲𝗿𝘁 𝗮 𝗳𝗼𝗿𝘄𝗮𝗿𝗱 𝘀𝗹𝗮𝘀𝗵 (/) 𝘁𝗼 𝗮 𝗵𝘆𝗽𝗵𝗲𝗻𝘀 (-).
As a system administrator, you may be tasked with changing the date formats from yyyy/mm/dd to the new format yyyy-mm-dd.
Here's an example of converting a forward slash (/) to a hyphen (-) and then appending the data to a file for storage.
If you want to learn Linux for FREE, open this (a thread of our Linux threads):
1. Permissions
The operating system controls file access in Linux by utilizing file permissions, attributes, and ownership. In Linux, file permissions, attributes, and ownership determine the level of access that system programs and users have to files.
If you want to learn Linux bash scripting for FREE, open this:
1. What is Shell in Linux
In this guide, let's learn about the shell which is an important part of the Linux operating system which interprets and passes the commands you provide on your keyboard to the operating system to perform some specific tasks
A variable is a named storage location in a program's memory where a value can be stored, retrieved, and manipulated. Like any programming language, Variables are an essential component in the Bash scripts.
Linux command line tools for parsing and analyzing logs 🐧↓
Linux logs are a valuable resource for system administrators, network administrators, developers, and security professionals.
They record a timeline of events on a Linux system, such as operating system events, application activity, and user activities and actions (for example log-in attempts).
Knowing when and how to stop running processes is an essential skill for sysadmins. When a process becomes stuck, it only takes a gentle nudge to restart or stop it.
At times, a process takes all the system resources. In both cases, you need a cmd that lets you manage a process.
The Linux operating system includes a number of commands for terminating errant processes (rogue processes), such as pkill, kill, and killall.
This thread will teach you how to use the kill command in Linux.
𝗚𝗲𝘁𝘁𝗶𝗻𝗴 𝘁𝗼 𝗸𝗻𝗼𝘄 𝗟𝗶𝗻𝘂𝘅 𝘀𝗶𝗴𝗻𝗮𝗹𝘀
Linux processes use signals to communicate with one another. A process signal is a predefined message that processes can either ignore or respond to. Developers define how a process will handle signals.
In Linux, most people use the ls command to check a file's creation, access, and modification times, ownership, and permissions.
What if I told you there is another great way to display detailed information about files and file systems?
Learn more about it in this thread:
The Linux ls command typically displays basic or chunk of information about a file; however, what if you want to print more information about the file? This is where the stat command comes in.
stat (short for status) is a command-line utility for displaying detailed information about specific files or file systems. It is commonly used to obtain file timestamps.