Include Column Headers When Piping Ps To Grep

Last modified: 
Tuesday, April 21st, 2015
Topics: 
sysadminLinux

How to view column headers when piping ps output to grep

This following line will output the column headers for ps aux and then write the results of ps aux | grep {whatever} below. This makes it easier to tell what you're looking at when reducing ps sets with grep.

In the example below we're probably working on a Mac and looking for the coreaudiod process. Perhaps it's crashed again or something. This will print out the column headers for ps followed by any lines containing the term coreaudiod which DO NOT contain the term grep. This prevents the grep process from your search from showing up in the output.

ps aux | head -1 && ps aux | grep coreaudiod | grep -v grep

USER             PID  %CPU %MEM      VSZ    RSS   TT  STAT STARTED      TIME COMMAND
_coreaudiod    62974   0.0  0.1  2455484   6100   ??  Ss    8:17AM   0:57.10 /usr/sbin/coreaudiod

Function to display column headers with ps aux output piped through grep

This is a little function you can place in your custom bash file to reproduce the above procedure.

function psaux {
  if [[ -n "$1" ]];then
    ps aux | head -1 && ps aux | grep "$1" | grep -v grep
  else
    echo 'You must supply a grep search expression!'
  fi
}

Usage

Simply call the function with your grep expression as the first argument. For example:

psaux coreaudiod


The operator of this site makes no claims, promises, or guarantees of the accuracy, completeness, originality, uniqueness, or even general adequacy of the contents herein and expressly disclaims liability for errors and omissions in the contents of this website.