Linuxopsys Profile picture
Oct 11 30 tweets 7 min read Twitter logo Read on Twitter
10 Linux tr command practical examples you should know as a system administrator (bookmark this): Image
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 " " Image
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:]" Image
𝟮. 𝗗𝗲𝗹𝗲𝘁𝗲 𝘀𝗽𝗲𝗰𝗶𝗳𝗶𝗰 𝗰𝗵𝗮𝗿𝗮𝗰𝘁𝗲𝗿𝘀

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 "-" Image
𝟯. 𝗗𝗲𝗹𝗲𝘁𝗲 𝗮𝗹𝗹 𝘁𝗵𝗲 𝗱𝗶𝗴𝗶𝘁𝘀

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:]" Image
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" Image
𝟰. 𝗖𝗮𝘀𝗲 𝗰𝗼𝗻𝘃𝗲𝗿𝘀𝗶𝗼𝗻

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:]" Image
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" Image
𝟱. 𝗥𝗲𝗺𝗼𝘃𝗶𝗻𝗴 𝗻𝗼𝗻 𝗮𝗹𝗽𝗵𝗮𝗻𝘂𝗺𝗲𝗿𝗶𝗰𝗮𝗹 𝗰𝗵𝗮𝗿𝗮𝗰𝘁𝗲𝗿𝘀

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:]" Image
𝟲. 𝗣𝗿𝗶𝗻𝘁 𝗲𝗮𝗰𝗵 𝘄𝗼𝗿𝗱 𝗼𝗻 𝗮 𝘀𝗶𝗻𝗴𝗹𝗲 𝗹𝗶𝗻𝗲

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" Image
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:]" Image
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 Image
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. Image
𝟵. 𝗣𝘂𝘁 𝗲𝗮𝗰𝗵 𝘄𝗼𝗿𝗱 𝗶𝗻 𝗮 𝗻𝗲𝘄 𝗹𝗶𝗻𝗲

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" Image
𝟭𝟬. 𝗖𝗼𝗻𝘃𝗲𝗿𝘁 𝗮 𝗳𝗼𝗿𝘄𝗮𝗿𝗱 𝘀𝗹𝗮𝘀𝗵 (/) 𝘁𝗼 𝗮 𝗵𝘆𝗽𝗵𝗲𝗻𝘀 (-).

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.

$ tr "/" "-" < old-date-format.txt > new-date-format.txt Image
𝗦𝘂𝗺𝗺𝗶𝗻𝗴 𝗨𝗽!

This thread demonstrated how to use the tr command with practical examples and its some available options for various text transformations.

If you get stuck with this command, feel free to refer to the man pages or the command help menu.
That’s all!

Hope you learn something new from this thread? If so, please let us know by replying in the comments.

If you're new here, do toss us a follow us (@linuxopsys) for more threads, tips and resources on Linux.
Also join 1000+ subscribers inside our newsletter "PenguinChronicles " and never miss any Linux, sysdmind and DevOps content:

linuxopsys.com/subscribe-news…

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Linuxopsys

Linuxopsys Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @linuxopsys

Oct 9
If you want to learn Linux for FREE, open this (a thread of our Linux threads): Image
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.

2. paste command in Linux explained

(simple yet powerful way to merge and organize data from multiple text files):

Learn more about the paste command in this master guide:

Read 41 tweets
Oct 8
If you want to learn Linux bash scripting for FREE, open this: Image
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

linuxopsys.com/topics/what-is…
Image
2. Bash variable assignment

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.

linuxopsys.com/topics/assign-…
Image
Read 56 tweets
Sep 27
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).
Read 26 tweets
Jul 22
The ability to link files is a fantastic feature of the Linux filesystem. Consider linking files in Linux to be similar to creating Windows shortcuts.

Links are extremely useful, especially if you need to have two or more versions of the same file on the system.

Learn more↓ Image
Instead of having multiple physical copies of the same file on the system, you can have one physical copy and numerous virtual copies, known as links.
A link is a placeholder in a directory that points to the file's actual location. In Linux, there are two types of file links:

• symbolic link
• hard link
Read 19 tweets
Jul 19
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. Image
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.
Read 33 tweets
Jul 15
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: Image
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.
Read 25 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(