A simple but handy Bash function: console

[Tweet : ADN : nvALT]

Here’s a quick riff on an older bash alias I use which will allow you to easily scan system messages from Terminal.

I’ve long had an alias in my ~/.bash_profile called console. It simply tailed the system log with ‘alias console=`tail -f /var/log/system.log’.

Bam

I decided to amp it up a little for added convenience. The function below allows me to add additional arguments which are translated into a regular expression for filtering the log output. Arguments, separated by spaces on the command line, are joined into a regular expression as OR matches. You can specify as many as you like to track each as a keyword. It’s case insensitive and you can use basic regular expression syntax in each parameter. Examples:

$ console byword

$ console usbmuxd servermgrd
		
$ console mark(ed|y) multimarkdown composer

console with no arguments will still just tail the log with no filter.

The function

Just stick this in your ~/.bash_profile file, run . ~/.bash_profile and try it out.

function console () {
  if [[ $# > 0 ]]; then
    query=$(echo "$*"|tr -s ' ' '|')
    tail -f /var/log/system.log|grep -i --color=auto -E "$query"
  else
    tail -f /var/log/system.log
  fi
}

If you want to track more than just the system.log file, you can just add additional filenames, separated by a space, after system.log for each tail command in the function.

There, that was handy.

Ryan Irelan has produced a series of shell trick videos based on BrettTerpstra.com posts. Readers can get 10% off using the coupon code TERPSTRA.