Top Command – How to Display Processes Matching a Specific Pattern

bashdebianprocessshelltop

I want to show all processes whose command line includes .*git.* using top.

I've considered the following command: top -p $(pgrep -d',' -f '.*git.*')

However, the above command has several caveats:

  • Only processes that exist while top is executed will be included in top's output (new .*git.*s are not included).
  • man top says that at most 20 processes may be specified using the -p flag, which may not be sufficient.

How can I show a dynamically top-like view of current .*git.* processes in the terminal?

Best Answer

You can do this in top using “filtering in a window”: start top, then press O, and enter

COMMAND=git

This will filter processes, matching the COMMAND column and keeping only processes whose visible value in that column contains git.

When filtering on the COMMAND column, the filter applies to the currently-displayed value — either the name of the running program, or its full command line. This can be toggled by pressing c, or with the -c argument; the state is remembered between top invocations. Note that the filter applies only to the visible portion of the field; this can be a significant gotcha when filtering on the COMMAND column, if it isn’t fully visible or if it isn’t wide enough to include the relevant part of the command line. See the COMMAND field description in man top for details.

Filters are cumulative and can be cleared by pressing =. See section 5e of man top for details.

As far as I’m aware there’s no equivalent command-line option.

Related Question