4 ms·
ps -ef | grep foobar is more easily done with pgrep foobar or perhaps pgrep -a foobar
by Symbiote 11y ago
ps -ef | grep foobar
is more easily done with
pgrep foobar
or perhaps
pgrep -a foobar
- SEJeff 11y agoor ps -ef | grep fo[o]bar So you don't have to | grep -v grep :) I ask people to explain what that does and how it works as a sysadmin interview question.
- nailer 11y agopgrep already excludes itself
- SEJeff 11y agoSure but that isn't often nearly as useful as something like: ps -efH | grep foob[a]r Where you can see the arguments and whatnot. Both are good to know. You can use awk's ability to do posix regex and emulate pgrep with: ps -efH | awk '/foob[a]r/{print $1}'
- notfoss 11y agoThat's nice. Apparently, we can place the brackets around any of the characters too! Can you explain how it works?
- SEJeff 11y agoSure, the square brackets in posix regex (used by grep) are for character ranges. You can wrap any single character in the brackets and it works the same. So foob[a]r matches the literal string foobar. If you grepped foob[ab]r it would match both literal strings foobar and foobbr. However, it doesn't match the literal string foob[a]r because [] is a character range. To match that, you'd need to escape it something like foob\[a\]r, which would not match the literal string foobar. This is why you don't need grep -v grep Understanding how and why this works will dramatically help you slice and dice text strings in a shell, so it makes a great SysAdmin interview question.