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
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
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop













{ 18 comments… read them below or add one }
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)
revactually 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-try:-
cat myfile | perl -e “print reverse “
tail -r myfiletail -r is not a valid command FYI
tail: invalid option — ‘r’
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
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.”
‘tac’ is the program anybody’d be looking for (In BSD it’s ‘gtac’)
Thank you , that’s i am looking
very usefull
10q
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-
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...
It does NOT work correctly because it adds a space character at the beginning of every line.
I used rev in:
find directory -type f | rev | sort | rev | xargs tar -Af archive.tarit gave me 122MB after gzipping instead of 128MB produced by
tar -czf archive.tgz directoryPoor man’s solid compression :).
Thanks a lot folk, Nice and usefull post
cat file | rev > elif
[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.
simple command to reverse the file contents as follow.
cat myfile | rev > otherfile
OR
rev myfile