Shell script reverse lines of a file

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

ADVERTISEMENTS

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
Output:

dnsmasq:x:111:65534:dnsmasq,,,:/var/lib/misc:/bin/false

Reverse one line

$ tail -1 /etc/passwd | rev
Output:

eslaf/nib/:csim/bil/rav/:,,,qsamsnd:43556:111:x:qsamsnd
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
18 comments… add one
  • soraver May 6, 2008 @ 12:32

    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)

  • xpmatteo Jul 22, 2008 @ 9:46

    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-

  • Scrix1 Aug 1, 2008 @ 10:45

    try:-

    cat myfile | perl -e “print reverse “

  • The Felis leo Aug 12, 2008 @ 12:44

    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.

    • Malcolm Jul 22, 2011 @ 18:02

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

      • Malcolm Jul 22, 2011 @ 18:04

        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

  • erik Nov 26, 2008 @ 12:32

    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.”

  • Snoodle Mynoodle Jan 13, 2009 @ 16:11

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

    • Chris Sep 15, 2011 @ 12:50

      Thank you , that’s i am looking

  • Adi Apr 24, 2009 @ 14:11

    very usefull
    10q

  • Ragu Apr 29, 2009 @ 18:59

    I found the awk command in google

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

    @
    http://librenix.com/?inode=6511

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

  • Markus Jun 18, 2009 @ 4:03

    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...

    • mp Mar 9, 2011 @ 10:39

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

  • m Dec 9, 2009 @ 17:40

    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 :).

  • jorgenorid Feb 8, 2010 @ 6:21

    Thanks a lot folk, Nice and usefull post

  • juke Oct 9, 2010 @ 7:41

    cat file | rev > elif

  • Anish Rana Jun 9, 2011 @ 7:34

    [aranas@bb-sas ~/perl]$ awk ‘BEGIN{FS=OFS=”.”}{print $7.$6$5,$4,$3,$2,$1}’ /etc/passwd | tail -1
    ….tacacs:*:4949:0:tacacs:/:/usr/sbin/nologin

    If you run this command it will reverse the columns.

  • Mayur Laniya Sep 6, 2011 @ 17:20

    simple command to reverse the file contents as follow.

    cat myfile | rev > otherfile
    OR
    rev myfile

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.