87

I like to use tac to reverse the output of cat. However, it's not available in the Mavericks terminal. I tried to find it on MacPorts and again it's not available. Can anyone please show me how to get tac? It's very helpful for reading log files.

4 Answers 4

102

On OS/X like on many systems (BSDs, Solaris, AIX, IRIX...), the functionality of GNU tac is available in tail with the -r option. So no need to install GNU tac:

tail -r the-file
4
  • 16
    So, alias tac='tail -r' will do the trick for OP!
    – Ketan
    Commented Feb 7, 2014 at 17:18
  • 13
    Well, actually using a shell alias may fail because aliases don't transfer into shell scripts. Use a function like tac() { tail -r -- "$@"; }
    – kojiro
    Commented Mar 1, 2016 at 17:44
  • Does it have options like -rs which will let you reverse at char level instead of new lines?
    – C--
    Commented Nov 7, 2019 at 20:00
  • @SubinSebastian, I've updated the link to the man page which Apple no longer makes available. But in short, no BSD tail has no equivalent of GNU tac's -s option. Commented Nov 7, 2019 at 21:45
91

Yes:

  • Install Homebrew
  • brew install coreutils
  • ln -s /usr/local/bin/gtac /usr/local/bin/tac apparently not needed with latest Homebrew, see comment by Ran Ever-Hadani below

or use MacPorts to install coreutils in a similar way.

2
  • 2
    Addition to grebneke's answer: no need the soft link anymore. The g suffix is now only added if osx already has a command with that name, so tac is installed as tac, not gtac. Commented Feb 17, 2019 at 21:34
  • 1
    The soft link is still needed when using MacPorts.
    – lenborje
    Commented Oct 12, 2023 at 9:02
1

One temporary solution could be:

alias tac='perl -e "print reverse(<>)"'
2
  • 1
    This just caused me to learn that <> doesn't only read from STDIN, but optionally from @ARGV. stackoverflow.com/questions/29020883/… I still don't know if reverse will load the whole file into memory before outputting the lines in reverse order. That would be terrible. Commented Apr 9, 2019 at 18:11
  • 1
    @BrunoBronosky, > ...STDIN, but optionally from @ ARGV : yes that is what <> is for. > ... will load the whole file into memory... : YES, IT WILL! (avoid doing this with gigabyte files)
    – JJoao
    Commented Apr 10, 2019 at 7:45
0

Install gnu coreutils already compiled with Rudix:

sudo rudix install coreutils

Or download and gui install Rudix coreutils

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .