How to reverse text file

This is just a brief note on how to reverse a text file. It is not really useful for anything other than fun, but it is stuck in my head, so I need to write it down.

I will use /etc/motd file as an example.

$ cat /etc/motd 

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

Display last line first.

$ cat /etc/motd | tac
permitted by applicable law.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent

individual files in /usr/share/doc/*/copyright.
the exact distribution terms for each program are described in the
The programs included with the Debian GNU/Linux system are free software;

Display last line first and reverse the order of characters in every line.

$ cat /etc/motd | tac | rev
.wal elbacilppa yb dettimrep
tnetxe eht ot ,YTNARRAW ON YLETULOSBA htiw semoc xuniL/UNG naibeD

.thgirypoc/*/cod/erahs/rsu/ ni selif laudividni
eht ni debircsed era margorp hcae rof smret noitubirtsid tcaxe eht
;erawtfos eerf era metsys xuniL/UNG naibeD eht htiw dedulcni smargorp ehT

The whole operation is reversible.

$ (cat /etc/motd | tac | rev) | tac | rev

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

The tac and rev applications are available in every Debian distribution.

$ dpkg-query -S $(which tac)
coreutils: /usr/bin/tac
$ dpkg-query -S $(which rev)
util-linux: /usr/bin/rev
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu