Shell script reverse lines of a file

Posted on in Categories , , , , , , , last updated October 16, 2007

Q. How do I reverse lines of a file under Linux / UNIX bash shell?

A. You need to use the rev utility or command. It copies the specified files to the standard output, reversing the order of characters in every line. If no files are specified, the standard input is read.

Display one line

tail -1 /etc/passwd


Reverse one line

$ tail -1 /etc/passwd | rev