TRÄW🤟 Profile picture
Oct 24 29 tweets 4 min read Twitter logo Read on Twitter
13 Linux tr command practical examples you should know as a system administrator: 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 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.

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

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 (@xtremepentest) for more threads, tips, and resources on Linux.
Also, join 1000+ subscribers inside our newsletter and never miss any Linux, sysadmin, and DevOps content:

sysxplore.substack.com

• • •

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

Keep Current with TRÄW🤟

TRÄW🤟 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 @xtremepentest

Jul 2
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:

$ useradd -D
Read 18 tweets
Jul 1
13 useful Linux find command practical examples 🧵↓
{ 1 } Find empty files and delete them:

This will delete all the empty files in the /home/ubuntu directory.

$ find /home/ubuntu/ -empty -delete
{ 2 } Find big files taking too much space so you can delete them blindly:

This will delete files which are greater than 47000k

$ find /home/ubuntu/ -type f -size +45000k -exec ls -l {} \; | awk '{ print $9 }' | xargs rm -i >/dev/null
Read 17 tweets
Feb 12
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.

udemy.com/course/linux-s…
2: Linux shell & scripting tutorial

Another great, free Udemy course to learn shell script core so you can get full potential at the command line.

udemy.com/course/linux-u…
Read 13 tweets
Feb 10
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
Read 38 tweets
Jan 6
Modern Linux boot process explained 🐧↓
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
Read 38 tweets
Jan 1
If you divide hacking into subparts, it becomes much easier to learn:

I created this road map to assist you in getting started. You are welcome to download and use it!
The Ultimate Hacker's Roadmap

1. Basic Computer Skills - Computer skills refer to the knowledge and abilities required to operate computers and related technology.
2. Basic Networking Skills - Networking skills are one of the most important talents to have if you want to be an ethical hacker.
Read 15 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!

:(