Shell script reverse lines of a file

by on October 16, 2007 · 18 comments· LAST UPDATED October 16, 2007

in , ,

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

Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 18 comments… read them below or add one }

1 soraver May 6, 2008 at 12:32 pm

and with tac (catin reverse) you can process a file from the end (reverse lines order)

i did A LOT of googling to find this out (a)


2 xpmatteo July 22, 2008 at 9:46 am

rev actually reverses characters, *not* lines. If you really want to reverse the *lines* of a file, you can use this:
cat -n myfile | sort -nr | cut -c 9-


3 Scrix1 August 1, 2008 at 10:45 am


cat myfile | perl -e “print reverse “


4 The Felis leo August 12, 2008 at 12:44 pm

tail -r myfile

-r Reverse. Copies lines from the specified starting point in the file in reverse order. The default for ‘r’ is to print the entire file in reverse order.


5 Malcolm July 22, 2011 at 6:02 pm

tail -r is not a valid command FYI
tail: invalid option — ‘r’


6 Malcolm July 22, 2011 at 6:04 pm

Seems Erik got to the source of it: it’s not supported in debian. I’m running Ubuntu; it’s not supported in here either.

tac is the best answer though: does exactly what we all want and is the simplest


7 erik November 26, 2008 at 12:32 pm

alas, in Linux Debian…

root@MyServer:/root# tail -r /etc/passwd
tail: invalid option — r
Try `tail –help’ for more information.

On AIX, I did find a version of tail that supports the -r option. Unfortunately, tail -r limits the output to only the last 20480 bytes of a file. If a file is larger, the output of tail is truncated. According to the man page of tail (man tail): “If the file is larger than 20,480 bytes, the -r flag displays only the last 20,480 bytes.”


8 Snoodle Mynoodle January 13, 2009 at 4:11 pm

‘tac’ is the program anybody’d be looking for (In BSD it’s ‘gtac’)


9 Chris September 15, 2011 at 12:50 pm

Thank you , that’s i am looking


10 Adi April 24, 2009 at 2:11 pm

very usefull


11 Ragu April 29, 2009 at 6:59 pm

I found the awk command in google

awk ‘{ a[NR]=$0 } END { for(i=NR; i; –i) print a[i] } ‘ temp1.txt > reversetemp1.txt


Thanks xpmatteo for the following command
cat -n myfile | sort -nr | cut -c 9-


12 Markus June 18, 2009 at 4:03 am

Works better:

cat -n temp| sort -nr|awk '{$1="";print}'

Tested on HPUX. tac and cat -r are not options on UNIX, probably they are part of GNU variants. The perl one-liner didn't work, but it should be easy to make one...


13 mp March 9, 2011 at 10:39 am

It does NOT work correctly because it adds a space character at the beginning of every line.


14 m December 9, 2009 at 5:40 pm

I used rev in:
find directory -type f | rev | sort | rev | xargs tar -Af archive.tar
it gave me 122MB after gzipping instead of 128MB produced by
tar -czf archive.tgz directory
Poor man’s solid compression :).


15 jorgenorid February 8, 2010 at 6:21 am

Thanks a lot folk, Nice and usefull post


16 juke October 9, 2010 at 7:41 am

cat file | rev > elif


17 Anish Rana June 9, 2011 at 7:34 am

[aranas@bb-sas ~/perl]$ awk ‘BEGIN{FS=OFS=”.”}{print $7.$6$5,$4,$3,$2,$1}’ /etc/passwd | tail -1

If you run this command it will reverse the columns.


18 Mayur Laniya September 6, 2011 at 5:20 pm

simple command to reverse the file contents as follow.

cat myfile | rev > otherfile
rev myfile


Leave a Comment

Tagged as: , , , , ,

Previous Faq:

Next Faq: