I have two simple questions regarding the use of the grep command.

What I want to do is:

  1. show the total lines (processes) running as me except for the command(s) that we used to create the listing.
  2. Next, I want to modify a listing of total lines running as me and only parse out the userID and the PID into a new list.

For the first one, I thought something along the lines of (ps -ef | grep myusername | grep -v "somecommand" | wc -l) would suffice but I'm not sure what to type in for someprocess to edit out the command used to generate the list.

For the second question, I'm not sure how to approach it.

  • You can tell ps to only show processes belonging to a certain user, no need to use grep for that. Also investigate the ps --format option for formatting the output according to your requirements.
    – wurtel
    Commented Feb 17, 2015 at 10:12
  • Well, (ps -ef | grep myusername) worked for grabbing just the processes under my username. Commented Feb 17, 2015 at 10:20
  • Sure, but is it the best solution... or is it what the homework assignment is asking for :-)
    – wurtel
    Commented Feb 17, 2015 at 10:30
  • It is what I was taught, to be honest. Haha. Commented Feb 17, 2015 at 10:34

3 Answers 3


To list all the processes by an user except for the command(s) that we used to create the listing, you can use:

ps -U username -u username u | grep -v ps

It list all the processes started by username as real user ID and effective user ID and filter the process ps to avoid to list the command used to create the listing.

The other one (user and PID):

ps -U username -u username u | grep -v ps | awk  {'print $1, $2'}

awk is used to to only show columns 1 and 2 of the results.

  • Excellent! I have yet to learn about the awk command unfortunately but I suppo se now is the best time! Could you perhaps briefly explain the logic of what is going on in the first one? I see that ps -U username -u username u lists every process running as username, I also see that grep -v selects all of the nonmatching lines to the ps command. Does that indicate that there is a process with ps in its name? Thanx! Commented Feb 17, 2015 at 10:33
  • wc -l counts all the lines including the title. (PID TTY TIME CMD)
    – jcbermu
    Commented Feb 17, 2015 at 10:52
  • THanx for the awk command explanation! You're correct. I forgot to add it into the comment. When I enter ps -u username | grep -v ps | wc -l into the command line it spits out that I have 7 processes. When I enter your command with | wc -l on the back, it returns 6. Could you enlighten me please? Haha. Commented Feb 17, 2015 at 10:57
  • You have to use -U username -u username to have the real and effective UID. Normally are the same. However if a program with a set-uid bit set is run, while the real UID remains that of the user who ran it, the effective UID is that of the user who owns the file.
    – jcbermu
    Commented Feb 17, 2015 at 11:18

For the first question you can use ps with below option to list all process that are run with your username.

$ ps -o pid,uid -u 1000|wc -l

You can replace the value 1000 with the UID of your user. I have added only pid and uid to get displayed. you can any any more of them.

For the second case is same as the first

$  ps -o pid,uid -u 1000 > process.list

For your first question, you can use the -c command to grep, which gives you a count of matches.

ps -ef | grep -v "grep" | grep -c "username"

This lists all processes, then removes the grep itself (don't count the one we are using to get the listing) and then get's a total count that are from your username.

  • For me, the ps command does not show up in my ps results. If it does for you, then you can add an additional grep -v "ps". It's usually a good habit to put what you are grepping for in quotes, so that when you grep for something with spaces or other characters, it will handle them correctly.
    – jrel
    Commented Feb 17, 2015 at 14:24

You must log in to answer this question.

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