Go to content Go to navigation Go to search

command line tips: top, less, ps

It’s amazing how much hidden information there is in documentation! I use a few tools all the time, but rarely venture outside of the command line arguments or key bindings I’m comfortable with. I’ve been trying to learn some of these extra features in hopes of saving a few seconds here and there. A few recent tips I’ve discovered:

less

First, make sure you actually use less. I’m always amazed when I’m watching over someone’s shoulder and they use more. That’s like using compress instead of gzip.

The command less +F file is a better version of tail -f. It watches the end of the file for changes and scrolls automatically, but allows you to use any other less feature at any time. Hit ^C to interrupt the tail mode, then scroll back or search for example.

While scrolling around you can mark a spot you’re interested in by pressing m, then any single letter. Pressing single quote ' then the letter again moves you back to that spot in the file.

You can less any number of files. Searching can work across all files. :n moves to the next file, :p to the previous.

You can search from the top of the first file, no matter where you are by pressing ^F after hitting / for search. ^F after ? means search from the last line of the last file. /* and ?* mean search across all files.

ps

For years I’ve used ps aux | grep ... to find what I want. Usually this is not right. a show processes for all users, so drop it if you only want to show what you’re running. u shows “user oriented output” like %CPU, %MEM. If you’re just trying to find the pid of what you want you don’t need that. x shows command line arguments. You can search for processes owned by a specific user with -u, like ps -u root.

The f argument is useful if you want to find a subprocess. It writes everything as an ASCII art tree. Once nice combo is ps fx | less +S. +S crops lines that are too long. This lets you see as much of the command line as possible without wrapping. A similar display with even better ASCII art is pstree -pa.

top

Top has probably hundreds of keys to do whatever you want. A few are most useful for me.

The > key sorts by the next column (%MEM) instead of the default (%CPU). < sorts by the previous column.

Hit c to toggle the display of command line arguments. Useful for me when I have a lot of processes that otherwise just show up as “java”

Typing A brings up the alternate display, showing processes sorted by different fields with different columns. One section shows a nice memory-centric view, for example.

Hitting z turns on colors. Z brings you to a color selection screen where you can pick colors you want. B turns on bold for some fields.

Hit W to save all your configuration changes to ~/.toprc where it will be loaded next time.

Type k allows you to kill a process without exiting top.

Commenting is closed for this article.