Linux command that lists processes one per line

Posted on

Problem :

Is there a way to restrict the output of, say
ps aux | grep word, so that each process is displayed on a separate single line?

I need this, because my processes descriptions take up a lot of lines and I don’t have good visibility when listing them with the above command. I would be happy to see just the first line of each process’ representation.

Solution :

Actually, each process is already displayed on a single line, that’s why grep shows that much info.

If you are willing to sacrifice the amount of details that the “COMMAND” column of ps aux offers, you may trim the output as such:

ps aux | awk '/word/ {print $1"t"$2"t"$3"t"$4"t"$9"t"$11}'

The awk looks for lines containing pattern ‘word’ and prints:

the USER, PID, %CPU, %MEM, START and first string (delimited by space)from the COMMAND column, all separated by TAB(‘t’)

You may find the pstree command useful. It lists all processes as a tree diagram.

When ps aux output is directed to a terminal, the command limits the command part to the width of the screen.

When the output is piped to a command, the behavior vary but usually, the width is unlimited. If you want to limit it the same way, you can use that command:

ps aux --witdh ${COLUMNS:-80} | grep word

Of course, if word isn’t in the displayed characters but in the truncated ones, this won’t fly.

Here are a couple of alternatives:

ps aux | awk '/word/ {print substr($0,1,'${COLUMNS:-80}')}'

ps -up $(pgrep -f word|tr 'n' ','| sed 's/,$//') 

Leave a Reply

Your email address will not be published. Required fields are marked *