LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   ps aux command to list only files in brackets (https://www.linuxquestions.org/questions/linux-newbie-8/ps-aux-command-to-list-only-files-in-brackets-4175442602/)

lovesuicide 12-22-2012 05:36 PM

ps aux command to list only files in brackets
 
Hi everybody,

I am looking to do the following: give a command line starting with ps aux that will print only entries with square brackets around the command field. I need to use a pipe after ps aux and keep in mind that the COMMAND field comes last in the output.

I believe I need to create a way to list files that are in brackets only. I've been able to create a command to exlclude files in brackets by typing the following:

grep -v ]

But I need to know how to create the opposite effect, and list only the files in brackets. Can anybody help?

Sorry for being such a beginner, but this problem has got me stumped.


Thanks!

lleb 12-22-2012 05:51 PM

try this, you might have to change the # value in the awk command, but this should get you started:

Code:

ps -aux | grep -v grep | awk '{print $11}' | grep -e ]
ps -aux = what you expect

grep -v grep = the -v excludes what is after the -v, in this case it will not display itself in the search

awk '{print $11}' = this will print ONLY the 11th field of the ps -aux

grep -e ] = this will look for ONLY matching the ] in the search.

hope that helps.

unSpawn 12-22-2012 06:37 PM

Code:

ps aux|awk '/\]/{print $11}'

lleb 12-22-2012 07:41 PM

now that is a much cleaner way of handling the search unSpawn. im learning. that is more to what i was wanting to do, but could not figure it out fast enough.

unSpawn 12-22-2012 10:18 PM

Then you might like David the H.'s advice and pointers here.

lovesuicide 01-08-2013 05:58 PM

thank you everybody! This accomplished exactly what I was looking to do.

Alucarx85 07-03-2013 02:46 PM

I was looking for that

Alucarx85 07-24-2013 02:22 PM

the right answer to this is ps aux|grep -e ]$

druuna 07-24-2013 02:41 PM

Quote:

Originally Posted by Alucarx85 (Post 4996079)
the right answer to this is ps aux|grep -e ]$

No it is not....

The OP wants the commands only, not all the other output:
Code:

root        1  0.0  0.0  8356  816 ?        Ss  07:50  0:01 init [2]

# vs

init [2]

Both answers given by lleb and unSpawn are not fully correct either; They only print field 11 and there can be more (see the above example).

Have a look at this:
Code:

ps ax -o args | grep -e "\]"

szboardstretcher 07-24-2013 02:57 PM

Quote:

Originally Posted by druuna (Post 4996090)
Have a look at this:
Code:

ps ax -o args | grep -e "\]"

I have to chime in, because grepping through the process list -- i do this often.

How about:

Code:

ps ax -o args | grep -e "\]" | grep -v grep

unSpawn 07-24-2013 03:26 PM

Quote:

Originally Posted by szboardstretcher (Post 4996104)
Code:

ps ax -o args | grep -e "\]" | grep -v grep

If you find yourself using 'ps|grep something|grep -v grep' a lot then know there's pgrep.


Quote:

Originally Posted by druuna (Post 4996090)
Both answers given by lleb and unSpawn are not fully correct either; They only print field 11 and there can be more (see the above example).

You're right.

*BTW if the objective would have been to find kernel threads then they're children of kthread (2.6) or kthreadd (3.x) which could be expressed as 'pgrep kthread; pgrep -P $(pgrep kthread)'. For user land processes pgrep works as well: 'pgrep -f "\[";'.

Alucarx85 09-20-2013 02:32 PM

if you work as an admin you need to resolve things quickly. that means you need to find a short way, less type and resolve things faster.

so I would stick with: ps aux|grep -e ]$


All times are GMT -5. The time now is 11:49 AM.