tr command in linux – description with examples

‘tr’ stands for ‘translate’. It is use to translate, like from lowercase to uppercase and vice versa or new lines into spaces.

Sample syntax with tr command :

# command | tr <‘old’> <‘old’>

Change Case

‘tr’ command can change case.

Sample syntax with tr command :

# command | tr <‘old’> <‘old’>

Sample syntax with tr command :

# command | tr <‘old’> <‘old’>

Like example of tr command :

# cat test | tr ‘prcu’ ‘PRCU’

See your terminal after run the command, p, r, c, u are convert into upper case P,R,C,U

Remove new lines with tr command

Write the all lines into a single line we have to translate all new lines into spaces.

Sample syntax with tr remove command :

# command | tr <‘\n’> <‘ ‘>

Like example of tr command :

# cat test | tr ‘\n’ ‘ ‘

tr Options

tr -s :

The command tr -s squeezes the occurrence of multiple characters into one.

Sample syntax with tr options command :

# command | tr -s <‘letter’>

Like example of tr -s command :

# cat test | tr -s ‘l’

# cat test | tr -s ‘ ‘

See your terminal after run the command, command “cat test | tr -s ‘l’ “ has squeezed all the letters ‘l’ into one and command “cat test | tr -s ‘ ‘” has squeezed all the spaces into single space.

tr rot13 :

This command encrypts the text. It is case-sensitive.

Like example of tr a-z command :

# cat test | tr ‘a-z’ ‘nopqrstuvwxyzabcdefghijklm’

# cat test | tr ‘a-z’ ‘n-za-m’

Look your terminal after run command, all the letters are encrypte according to the command. But letter ‘A’ and ‘M’ are not encrypte as they are in upper case.

tr -d:

‘tr -d’ command is use to delete characters.

Sample syntax with tr -d command :

# command | tr -d <letter>

Like example of tr a-z command :

# cat test | tr -d o

Look your terminal after run the command, all the ‘o’ letters are delete from the file ‘test’.

Please practice more and more then you will gain expertness.

Leave a Reply