1

I'm trying to grep for processes that originate from the /Applications/ directory (to kill them), but my grep command keeps catching some false ones as well. What am I doing wrong here?

$ ps -ef | grep -e '/Applications/' | grep -v grep | awk '{print $8}'
/Library/Application        I DON'T WANT THIS
/Library/Application        I DON'T WANT THIS
/Applications/Firefox.app/Contents/MacOS/firefox
/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal
/Applications/Notes.app/Contents/MacOS/Notes

3 Answers 3

1

Use the anchor ^ (caret symbol), to signify the beginning of the line/field and change the command order:

 ps -ef | grep -v grep | awk '{print $8}' | grep "^/Applications"

This way, grep will run last and will only return the lines that start with /Applications.

This is also helpful.

1
  • I'm not sure how many times I played around with the caret, but looking at your pipe arrangement, I see now where I did wrong. Thanks! Jun 3, 2014 at 21:23
1

If you are bothering about saving CPU ticks:

ps -ef | awk '$8~"^/Applications/"{print $8}'
0
0

Try including a whitespace in your pattern:

...  grep -e ' /Applications/'  ...

Also, pkill (long a standard offering in Linux) is now included in OS X Mountain Lion (10.8) and above. See http://osxdaily.com/2012/10/18/kill-process-wildcards-pkill-mac-os-x/

3
  • Thank you so much for mentioning pkill. I think it will serve exactly what I need right now, which is to terminate any apps from the /Applications/ directory. I tried the following and it works. It even excludes Terminal.app on its own: pkill -f Applications Haven't play around with this command much, but I will have to see if it needs to be grep-ed or anything. I'll mark you as my accepted answer since you went above and beyond my needs. Jun 3, 2014 at 21:17
  • @StéphaneChazelas good point. Edited the answer and removed the escaping on the whitespace. OP liked the pkill more than the grep/whitespace tip anyhow. Thanks. Jun 3, 2014 at 22:18
  • Note that pgrep/pkill originated in Solaris, not Linux, and were added to BSDs (NetBSD first) around the same time as Linux' procps (early 2002). Jun 4, 2014 at 5:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .