0

When I pipe to grep after a ps aux command it isn't showing the categories at the top of the list (USER, PID, %CPU, %MEM, etc) Is there something I can do about this?

ps aux --sort -rss | grep $USER | head -n 4
user01      1610  0.0  0.3  17968 10156 ?        Ss   Jan19   0:01 /lib/systemd/systemd --user

user01 1611 0.0 0.0 104400 2108 ? S Jan19 0:00 (sd-pam)

user01 1617 0.0 0.1 48216 4812 ? S<sl Jan19 0:00 /usr/bin/pipewire

user01 1618 0.0 0.1 32108 4256 ? Ssl Jan19 0:00 /usr/bin/pipewire-media-session

I am expecting to see the following at the very top:

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

1 Answers1

0

Aside from the issue of omitting the header line, piping ps aux output through grep $USER is an unreliable way to select only processes owned by your user, it may:

  • not match at all, since the USER column may be truncated

  • match elsewhere in the output line (such as a command name or argument)

You could do something hacky like ps aux --sort -rss | grep -E "^(${USER:0:7}|USER)" | head -n 5, however if you want only the current user's processes you can simply omit the a option from your ps command:

$ ps ux --sort -rss | head -n 5
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
steeldr+    1279  0.0  4.5 416748 44804 ?        Rl   03:27   0:12 /usr/bin/qterminal
steeldr+    1102  0.0  4.2 907696 42420 ?        Sl   03:27   0:11 /usr/bin/lxqt-panel
steeldr+    1095  0.0  3.9 853820 39056 ?        Sl   03:27   0:00 /usr/bin/pcmanfm-qt --desktop --profile=lxqt
steeldr+    1101  0.0  2.2 405672 22252 ?        Sl   03:27   0:00 /usr/bin/lxqt-notificationd
steeldriver
  • 136,215
  • 21
  • 243
  • 336