Bash – How to Reverse Text File in Bash

bashshell

How can I reverse a text file in Bash?

For example, if some.txt contains this:

book
pencil
ruler

then how can I get this? :

relur
licnep
koob

Best Answer

Try the combined form of tac and rev commands,

$ tac file | rev
relur
licnep
koob

From man tac

tac - concatenate and print files in reverse

From man rev

The rev utility copies the specified files to standard output, reversing the order of characters in every line. If no files are specified, stan‐ dard input is read.

Related Question