r/bashtricks • u/wishmegood • Dec 19 '19
Grep only once on ps
When looking for a process but get two of them
$ ps -ef | grep firefox
user1 2330 1 1 Jan29 ? 00:27:27 /usr/lib/firefox/firefox ...
user1 15820 618 0 03:58 pts/1 00:00:00 grep --color=auto firefox
try
$ ps -ef | grep [f]irefox
user1 2330 1 1 Jan29 ? 00:27:27 /usr/lib/firefox/firefox ...
Now you don't get bothered by extra grep or don't need to do grep -v grep ..
The command grep [a-zA-Z0-9]irefox would even find all processes that start with exactly one letter or number and end in "irefox".
6
Upvotes
1
u/wishmegood Dec 20 '19
Maybe if you try command with [] instead of command line parameter
2
u/dajobe Dec 20 '19
pgrep -f firefox
1
u/DopplegangerNZ Dec 19 '19
I've given it a go and see that it will exclude the 'grep --color' line, but I don't get why.
[] = match any one of the enclosed characters, as in [aeiou]. Use Hyphen "-" for a rangeAn example:
Shouldn't [u]nix still match the 'unix' in the 'grep --color' line?
1
u/[deleted] Feb 03 '20
just give grep -v grep at the end.
so your command should be ps -ef | grep firefox | grep -v grep
read man page about -v