cat a file from end to beginning

Is there an option, for cat, head, tail, or is there any way, to display a file from last line to first? For example, my file
looks like this:

aaaa
bbbb
cccc
eeee

and I would like to print or display it like this:

eeee
cccc
bbbb
aaaa

thanks

If file contains;
aaaa
bbbb
cccc
dddd

Then

sort -r file

will output;
dddd
cccc
bbbb
aaaa

This will only work if the original file is in alphabetical order. The proper solution is to use 'tac' (which is the opposite of 'cat').

or cat file | perl -e 'print reverse <>'

Just do perl -e 'print reverse <>' file

However, the down side to the perl method is that the whole file must be read into memory. Could be a memory-hog for extremely large files. I believe 'tac' uses seek to read through the file, so that you can view files of arbitrary size. Usually not a problem though. The perl method is good if you dont have 'tac' installed.

Yes, use of cat there shows useless usage of cat. :slight_smile:

Thanks for pinch on nose warning PxT.