13 Linux tr command practical examples you should know as a system administrator:
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 space characters into a single character.
$ echo "Linux is awesome, I'm in love with it." | tr -s " "
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 the 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 semicolons 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.
Linux user management - how to add users in Linux 🧵↓
The useradd command is the main tool for adding new users to a Linux system. This command allows you to quickly create a new user account and configure the user's $HOME directory structure.
The useradd command creates a user account by combining system default settings and command-line parameters.
To see the system default values on your Linux distribution, use the useradd command with the -D option:
If you want to learn bash scripting and start automating tasks, open this:
1: Intro to Linux Shell Scripting (Free course)
This mini-course is designed to help beginners quickly become proficient in Linux shell scripting in only one hour. It is ideal for those who are just starting with a bash shell.
Modern Linux boot process explained (detailed thread)🐧↓
When you turn on your Linux computer, it goes through a series of phases before presenting a login screen that prompts you for your username or password.
Every Linux distribution goes through four distinct stages during the boot-up process.
The booting process consists of four steps, which we will go over in this thread:
• BIOS and UEFI Integrity check (POST)
• Loading of the Boot loader (GRUB2)
• Kernel initialization
• Starting systemd, the parent of all processes
When you turn on your Linux computer, it goes through a series of phases before presenting a login screen that prompts you for your username or password.
Every Linux distribution goes through four distinct stages during the boot-up process.
The booting process consists of four steps, which we will go over in this thread:
• BIOS and UEFI Integrity check (POST)
• Loading of the Boot loader (GRUB2)
• Kernel initialization
• Starting systemd, the parent of all processes