psg (ps grep) function if you don't have pgrep or don't know how to use it

psg() { if [ -z "$2" ]; then psargs="aux"; greparg="$1"; else psargs="$1"; greparg="$2"; fi; ps $psargs | grep -i "$(echo $greparg | sed -e 's/^\(.\)/[\1]/')\|^$(ps $psargs | head -1)" ; }
My variant on this common function. Some highlights: Allows you to override the default ps args of "aux" Uses bracket trick to omit the grep process itself without having to use a second grep Always prints the correct header row of ps output Limitations: Ugly ps error output if you forget to quote your multi word grep argument
Sample Output
den:~ dre$ psg ssh
dre      45705   0.0  0.0    75672   1000 s001  S+    2:44PM   0:00.04 ssh deinspanjer@server1
dre      45687   0.0  0.0    75736   1032 s000  S+    2:38PM   0:00.35 ssh deinspanjer@server2
dre        247   0.0  0.0    76764    840   ??  S     8Feb09   0:03.68 /usr/bin/ssh-agent -l

2009-02-18 20:57:17

What Others Think

Some systems have similar utilities such as pgrep.
jsiren · 526 weeks and 2 days ago
And then... some don't. Is that any reason to downvote a script that is useful to people not blessed with pgrep?
DEinspanjer · 526 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: