Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When I search for some process that doesn't exist, e.g.

$ ps aux | grep fnord                          
wayne    15745  0.0  0.0  13580   928 pts/6    S+   03:58   0:00 grep fnord

Obviously I don't care about grep - that makes as much sense as searching for the ps process!

How can I prevent grep from showing up in the results?

share|improve this question
9  
If you need only the PID of a process, you can replace ps aux |grep with pgrep (or pgrep -f ). –  jofel Apr 30 '13 at 13:54
6  
Same question on Serverfault and Superuser. –  Thor Apr 30 '13 at 14:08
    
is that output legit? it seems that there IS no program named fnord running... –  acolyte Apr 30 '13 at 17:10
2  
@acolyte, that's precisely it - but because it's piping the output into grep, grep is running (waiting for the output of ps aux, I expect). So the question is how to prevent grep fnord from showing up as a running process because obviously I'm not interested in that one. –  Wayne Werner May 2 '13 at 1:58
1  
@acolyte it's not a surprise that you cannot see the "fnord" line. You are not supposed to be able to see it. If you have some 2 or 3 minutes to spare, search for it. –  Francesco May 2 '13 at 9:54

8 Answers 8

up vote 94 down vote accepted

Turns out there's a solution found in keychain.

$ ps aux | grep "[f]nord"

By putting the brackets around the letter and quotes around the string you search for the regex, which says, "Find the character 'f' followed by 'nord'."

But since you put the brackets in the pattern 'f' is now followed by ']', so grep won't show up in the results list. Neato!

share|improve this answer
5  
@acolyte: The output from ps will include a line ending with grep [f]nord. However, that line will not make in through the grep filter, because the string [f]nord does not match the regular expression [f]nord. –  LarsH Apr 30 '13 at 17:58
5  
@LarsH interesting, thanks! i would have suggested ps aux | grep fnord | grep -v grep ...which seems to have already been suggested by a few people...lol –  acolyte Apr 30 '13 at 18:16
2  
This is a very old unix trick. I first saw it 15+ years or so ago. –  slm May 1 '13 at 1:39
2  
@progo Yes, you do need to quote the [ even in bash. Try it with a file called fnord in the current directory. –  Gilles May 1 '13 at 22:18
6  
Grepping the output of ps may be an old trick, but it's unreliable. Use pgrep if available. –  Gilles May 1 '13 at 22:19

Another option I use (especially just to look if a process is running) is the pgrep command. This will search for a matching process, but not list a grep line for the search. I like it because it is a quick way of search, without regexing or escaping anything.

pgrep fnord
share|improve this answer
    
They will want the -f option to be more like ps | grep. –  jordanm Apr 30 '13 at 14:16
    
@jordanm Actually, pgrep -f will look more like ps -f | grep. –  michelpm Apr 30 '13 at 18:09
    
There is also the -l argument to make it show the command that's being matched. –  Patrick May 1 '13 at 3:34
3  
ps | grep '[f]nord' is clever and venerable, but pgrep is right. –  kojiro May 1 '13 at 11:33
1  
You can always do ps $(pgrep cmd) ... if `pgrep is missing options (it won't work for empty set of commands though). –  Maciej Piechotka May 1 '13 at 14:18

The ideal solution is the one presented by BriGuy

pgrep fnord 

But if you do not want to do that, you can just exclude all lines that matches with grep with:

ps aux | grep -v grep | grep "fnord"
share|improve this answer
4  
but what if the line I'm looking for is "bash bash grep bash"? –  Sparr Apr 30 '13 at 20:27
    
@Sparr Then you should use the best solution: pgrep or find another one ;) –  RSFalcon7 May 1 '13 at 15:15

In zsh, grep fnord =(ps aux).

The idea is, first run ps aux, put the result in a file, then use grep on that file. Only, we don't have a file, as we use zsh's "process substitution".

To illustrate, try

ps aux > ps.txt
grep fnord ps.txt
rm ps.txt

The result should be the same.

General comment on some of the other answers. Some are far to complicated and/or long to type. It is not only a matter of being right, it should be usable as well. But that doesn't mean some of those solutions are bad; only, they must be wrapped within a mini-UI to make them usable.

share|improve this answer
1  
Not knowing zsh's process substitution, but is it certain that the two processes are called one after the other and not in parallel? –  Paŭlo Ebermann May 1 '13 at 10:17
2  
@PaŭloEbermann: For parallel is the <(command). grep grep =(ps aux) doesn't show any line. –  Maciej Piechotka May 1 '13 at 14:17

Not the most elegant solution but you could do this:

$ ps aux | grep fnord | grep -v grep

share|improve this answer
ps aux | grep $(echo fnord | sed "s/^\(.\)/[\1]/g")
share|improve this answer
1  
this looks like an IOCCC, but for unix command line instead of C ^^ Just | grep $(echo fnord) wasn't enough ? you also ask sed to search and replace each char in "fnord" by its own value? ^^ congrats. I bet I can do an even longer one, but it probably won't be as funny ^^ –  Olivier Dulac Apr 30 '13 at 16:26
    
I apologize as the tone of my above comment may sound offensive (and I can't edit it after 5mn)... Your answer was really a joyful read, and I wasn't trying to make you look bad. I really think you did it like this on purpose ^^ –  Olivier Dulac Apr 30 '13 at 16:31
1  
Don't apologize, your answer was really fun to read (and spot-on right too). I meant pgrep -l $1 anyway ;) –  xaccrocheur Apr 30 '13 at 16:48
2  
@OlivierDulac: He doesn't just replace the first char with its own value; he replaces it with the concatenation of its value among square brackets. (Those square brackets are not special, they're literal.) Basically, he's generalizing on Wayne Werner's answer, so that the process of putting square brackets around the first character can be scripted. +1 –  LarsH Apr 30 '13 at 18:02
1  
ps aux | grep '[^]]fnord' would avoid that gymnastic. –  Stéphane Chazelas Apr 30 '13 at 20:21

Maybe time to use a real sequence this time. The use of pipes makes it parallel.

ps aux >f && grep tmpfs <f

Ugly because there will be a file f, but it's not my fault that there is no syntax for sequential processes where you still want to use the output of a previously run process.

Suggestion for the syntax of a sequential operator:

ps aux ||| grep tmpfs
share|improve this answer
    
The inability of the common people to easily filter a stream is hardly a reason for the elite to base themselves with sequences. It would be a step backward in computing. –  A-B-B Aug 18 at 20:27
    
@A-B-B en.wikipedia.org/wiki/Communicating_sequential_processes have many more operators than those that ended up in the Thompson shell. There could just have been an operator that is sequential, and which communicated its result to the next process in the sequence as soon as its exits. A "step backward in computing" by providing an additional operator seems a bit harsh. –  Anne van Rossum Aug 19 at 7:09
    
Nice link, but of course sequential processing is worse in both CPU and memory (or disk) use -- relative to parallel processing. In this instance, if I must first store all ps output before I grep, I'm using more memory (or disk) for longer. –  A-B-B Aug 19 at 19:37

The pgrep command, as others have stated, will return the PID (process ID) of processes based on name and other attributes. For example,

pgrep -d, -u <username> <string>

will give you the PIDs, delimited by a comma (,) of all processes whose name matches being run by user <username>. You can use the -x switch before to return only exact matches.

If you want to get more information about these processes (as running the aux options from ps implies), you can use the -p option with ps, which matches based on PID. So, for example,

ps up $(pgrep -d, -u <username> <string>)

will give detailed information of all the PIDs matched by the pgrep command.

share|improve this answer
    
ps fails if pgrep returns an empty set. I have an answer which builds upon yours and tries to address this issue. –  A-B-B Aug 19 at 17:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.