Limiting TOP command to a specific process name and keeping interactivity


In this post we analyze the following command which allows you to run “top interactively without having to look up the pids” your interested in.

top -p `pgrep process-name | tr "\\n" "," | sed 's/,$//'`


The command is split into multiple sub commands.

  • top: displays running processes. With -p option  means monitor specific process IDs.
  • pgrep: Looks through the currently running processes and lists the process IDs which matches the selection criteria (proccess-name).
  • pipe (|): The pipe operator. It is used to direct the stdout of the first command to the stdin of the second command.
  • tr (translate): Is used for replacing or removing specific characters in its input data set. Above: linefeed(\n) is replaced with comma(,) .
  • sed: sed is used to remove the last comma(,) for the list. Sed is a stream editor which are used to perform basic text transformations on an input stream (a file or input from a pipeline). sed‘s ability to filter text in a pipeline particularly distinguishes it from other types of editors.



$ pgrep ini

piping pgrep result to tr for replacing linefeed with ,:

$ pgrep ini | tr "\\n" ","
$ pgrep ini | tr "\\n" "|"

using sed to remove trailing , :

$ pgrep ini | tr "\\n" "," | sed 's/,$//'

example of top with process list:

$ top -p 1,1789,27123


Menelaos Bakopoulos

Mr. Menelaos Bakopoulos is currently pursuing his PhD both at Center for TeleInFrastruktur (CTiF) at Aalborg University (AAU) in Denmark and Athens Information Technology (AIT) in Athens, Greece. He received a Master in Information Technology and Telecommunications Systems from Athens Information Technology and a B.Sc. in Computer Science & Management Information Systems from the American College of Thessaloniki. Since April 2008 he has been a member of the Multimedia, Knowledge, and Web Technologies Group.

More Posts