This is something i wrote for work but figure it could be pretty general purpose… Read the postMaking the Most of Terminal