Jacob Tomlinson's profile picture Jacob Tomlinson
Home Blog Talks Newsletter About

Sort top command by cpu usage and set to default in OS X

2 minute read #alias, #bash, #os-x, #terminal, #top

As I come from a linux background but seem to spend more and more of my free time using OS X I keep noticing little differences in the way the command line works on a mac. One difference which as been bugging me recently is that way that the top command orders itself. I’m used to having it ordered by highest processor usage at the top on linux, which I find the most useful as generally when I run top I’m looking to see what is chewing up my cpu. However when you run top in OS X it orders by pid, so the newest processes are at the top.

To change this you can give some extra parameters to set the ordering. So to order by cpu on a mac you give the command

top -o cpu

which is great but I don’t really want to be typing this in every time, I want it to be the default.

To do this you can create an alias in your ‘.profile’ file. This file should be in your home directory but if you don’t have one you can just create one with the command

touch ~/.profile

In there you just need to add the line

alias top="top -o cpu"

this means when you run the command top it will call top -o cpu instead. To apply these changes you can either type . .profile or just close the terminal and open it again. You can run the default top command by sticking a ** before the command which will ignore the alias, more details on setting an alias here.

There are of course other ways around this, one being to use the more advanced and powerful htop command which can be installed with macports or brew. But for a quick little workaround this does the trick for me.

Have thoughts?

I love hearing feedback on my posts. You should head over to Twitter and let me know what you think!

Spotted a mistake? Why not suggest an edit!