top -c command in linux to filter processes listed based on processname

user1615330 picture user1615330 · Aug 22, 2012 · Viewed 188.7k times · Source
top -c

Top lists all the processes, there are good options to filter the processes by username by using the option -u but I am wondering if there is any easy way to filter the processes based on processname listed under COMMAND column of the top output.

For Example I would want like top -some option -substring of processname and top displays pids only having this substring in its command name

Answer

perreal picture perreal · Aug 22, 2012

Using pgrep to get pid's of matching command lines:

top -c -p $(pgrep -d',' -f string_to_match_in_cmd_line)

top -p expects a comma separated list of pids so we use -d',' in pgrep. The -f flag in pgrep makes it match the command line instead of program name.