20

I know that we can easily find the following:

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

of all the running processes by using the command:

sudo ps aux

My, question is that, is it possible to show the output of only the column under COMMAND and nothing else?

Raphael
  • 7,985
  • 5
  • 34
  • 51

3 Answers3

27

Use -o flag.

To list only your own processes

 ps -o command  

To list all system processes

 ps -e -o command

This is not the only way to list commands, they can either be printed with command line flags, or as executable only (which is what command option does).

From my comment bellow the answer:

command, args, and cmd all give full command. In fact man page states command and cmd are aliases for args , with the - flags included. The comm gives the name of the exacutable only. Aliases to that are ucmd and ucomm. I misread about AIX options, those can be actually specified with printf-like format, %a for args, %c for comm

Refer to man ps for more info on usage and available format options

Programmatic approach would be a bit redundant , since ps already provides us with the formatting options, but it can be done with awk, which is much useful when dealing with columnized output.

ps aux | awk '{ for(i=1;i<=NF;i++) {if ( i >= 11 ) printf $i" "}; printf "\n" }'

Note however, that this code breaks if username contains whitespace, e.g. john doe. This can be amended with adding gsub function that will elimiate your username from the ps list. However, if there is multiple users logged into system, that may be difficult to errase all of the usernames from the output. Thus, you can see that -o flag is much more preferred.

Side note, sudo is not necessary to for listing all processes with ps

Sergiy Kolodyazhnyy
  • 103,293
  • 19
  • 273
  • 492
  • Thank You! Are there any differences between `ps -eo command` & `ps -eo comm`? The only difference I found was in `command` the output was as such: `[kthreadd]` whereas in `comm` the output was as this: `kthreadd`. Does this signify anything? – Raphael Nov 18 '15 at 07:19
  • @Raphael according to the man page, the difference is that `comm` is AIX format, where as `command` is standard GNU style format. AIX is another *NIX like system, so that option may be used for compatability. Otherwise, no difference – Sergiy Kolodyazhnyy Nov 18 '15 at 07:24
  • @Serg, I just executed the `diff <(ps -eo comm) <(ps -eo command)` and there seems to be quite a rattle down there, can you clarify? – Andrew Nov 18 '15 at 07:42
  • Watch out, the `awk` solution breaks if an username contains spaces ([here](http://askubuntu.com/questions/699439/output-only-the-column-under-command-of-ps-command-in-terminal/699447?noredirect=1#comment1024950_699447)); also maybe just `ps aux | awk '{ for(i=11;i<=NF;i++) {printf $i" "}; printf "\n" }'`? – kos Nov 18 '15 at 07:59
  • @kos yeah, I should have started loop at 11th field anyway,no need for if statement. I will add that note to my answer. Perhaps gsub would help us eliminate the username ? – Sergiy Kolodyazhnyy Nov 18 '15 at 08:03
  • Yes I think so. That way at least it should be always the 10th field. – kos Nov 18 '15 at 08:07
  • 2
    @Andrew So, I've read the manual a little more. So `command`, `args`, and `cmd` all give full command. In fact man page states `command` and `cmd` are aliases for `args` , with the `-` flags included. The `comm` gives the name of the exacutable only. Aliases to that are `ucmd` and `ucomm`. I misread about AIX options, those can be actually specified with printf-like format, %a for args, %c for comm – Sergiy Kolodyazhnyy Nov 18 '15 at 08:08
  • Added the comment and some edits to my answer – Sergiy Kolodyazhnyy Nov 18 '15 at 08:13
  • @Serg, your answer sounded greek and latin and I did some homework on the ps' man page and I never thought `ps` could do so much on its own. I agree with `ps -o` suiting all situtations. – Andrew Nov 18 '15 at 08:19
  • @Andrew sorry, that's a pitfall of using technical vocab too much. Basically, `command` = `args` = `cmd`, and they all show full command. Example, it would show `myScript.sh -flag 1 -flag 2`. Whereas, `comm` only shows `myScript.sh` – Sergiy Kolodyazhnyy Nov 18 '15 at 08:22
  • Yeah, I just looked them up at the bottom of the man page. It says `arg` contains the _command_ with all arguments while `comm` displays the _command alone_ as you had illustrated in your examples, thanks! Wondering why `args` has so many aliases... – Andrew Nov 18 '15 at 08:34
  • @Andrew most likely due to the fact that `ps` command differed from system to system: there's BSD options in there, AIX, probably others. It's not only `ps` thing , many other programs have extra options for compatibility – Sergiy Kolodyazhnyy Nov 18 '15 at 08:37
  • I have a book on System V Unix , which was used in like 80s and its implementation of ps would show only cropped command by default,but full command with arguments if you used `-f` flag – Sergiy Kolodyazhnyy Nov 18 '15 at 08:40
  • @Serg, yeah using it for several platforms makes it bulky! And in the description of `ps` on the man page, there is a line that says subject to change! I thought this was de-facto. I think the older version would've been better... and now `-f` must be obsolete. – Andrew Nov 18 '15 at 08:58
  • If yo want to exclude header (`COMMAND`), just add symbol `=` (i.e. `ps -o command=`) or use option `--no-header` (the latter may be unsupported). – kirikaza Apr 27 '18 at 06:55
  • Thank you worked! – Smeterlink Aug 02 '20 at 10:04
6

A more generic way. We determine the column of the command. In this way, it does not matter which switches are used for ps.

ps ax | awk -v p='COMMAND' 'NR==1 {n=index($0, p); next} {print substr($0, n)}'

Sample output

% ps aux | awk -v p='COMMAND' 'NR==1 {n=index($0, p); next} {print substr($0, n)}'
/sbin/init splash
[kthreadd]
[ksoftirqd/0]
[kworker/0:0H]
[rcu_sched]
[rcu_bh]
[migration/0]
[watchdog/0]
[watchdog/1]
[migration/1]
[ksoftirqd/1]
[kworker/1:0H]
[watchdog/2]
[migration/2]
[ksoftirqd/2]
[kworker/2:0H]
[khelper]
[kdevtmpfs]
[netns]
[perf]
[khungtaskd]
[writeback]
[ksmd]
[khugepaged]
[crypto]
[kintegrityd]
[bioset]
[kblockd]
[ata_sff]
[md]
[devfreq_wq]
[kswapd0]
[fsnotify_mark]
[ecryptfs-kthrea]
[kthrotld]
[acpi_thermal_pm]
[scsi_eh_0]
[scsi_tmf_0]
[scsi_eh_1]
[scsi_tmf_1]
[ipv6_addrconf]
[deferwq]
[charger_manager]
[kpsmoused]
[scsi_eh_2]
[scsi_tmf_2]
[kworker/0:1H]
[jbd2/sda1-8]
[ext4-rsv-conver]
[kauditd]
[kworker/0:2]
/lib/systemd/systemd-journald
/lib/systemd/systemd-udevd
[…]

And what's about pstree?

Sample output

% pstree -alU
systemd splash
  ├─ModemManager
  │   ├─{gdbus}
  │   └─{gmain}
  ├─NetworkManager --no-daemon
  │   ├─dhclient -d -q -sf /usr/lib/NetworkManager/nm-dhcp-helper -pf /run/sendsigs.omit.d/network-manager.dhclient-eth0.pid -lf /var/lib/NetworkManager/dhclient-57cfb005-1dc6-410d-aebc-16d8d152abfd-eth0.lease -cf /var/lib/NetworkManager/dhclient-eth0.conf eth0
  │   ├─dnsmasq --no-resolv --keep-in-foreground --no-hosts --bind-interfaces --pid-file=/run/sendsigs.omit.d/network-manager.dnsmasq.pid --listen-address=127.0.1.1 --conf-file=/var/run/NetworkManager/dnsmasq.conf --cache-size=0 --proxy-dnssec --enable-dbus=org.freedesktop.NetworkManager.dnsmasq --conf-dir=/etc/NetworkManager/dnsmasq.d
  │   ├─{gdbus}
  │   └─{gmain}
  ├─VBoxClient --clipboard
  │   └─VBoxClient --clipboard
  │       └─{SHCLIP}
  ├─VBoxClient --display
  │   └─VBoxClient --display
  │       └─{VT_MONITOR}
  ├─VBoxClient --seamless
  │   └─VBoxClient --seamless
  │       └─{X11 events}
  ├─VBoxClient --draganddrop
  │   └─VBoxClient --draganddrop
  │       ├─{dndHGCM}
  │       └─{dndX11}
  ├─VBoxService --pidfile /var/run/vboxadd-service.pid
  │   ├─{automount}
  │   ├─{control}
  │   ├─{cpuhotplug}
  │   ├─{memballoon}
  │   ├─{timesync}
  │   ├─{vminfo}
  │   └─{vmstats}
  ├─VBoxService
  │   ├─{automount}
  │   ├─{control}
  │   ├─{cpuhotplug}
  │   ├─{timesync}
  │   ├─{vminfo}
  │   └─{vmstats}
  ├─accounts-daemon
  │   ├─{gdbus}
  │   └─{gmain}
  ├─agetty --noclear tty1 linux
  ├─apache2 -k start
  │   ├─apache2 -k start
  │   ├─apache2 -k start
  │   ├─apache2 -k start
  │   ├─apache2 -k start
  │   └─apache2 -k start
  ├─at-spi-bus-laun
  │   ├─dbus-daemon --config-file=/etc/at-spi2/accessibility.conf --nofork --print-address 3
  │   ├─{dconf worker}
  │   ├─{gdbus}
  │   └─{gmain}
  ├─at-spi2-registr --use-gnome-session
  │   ├─{gdbus}
  │   └─{gmain}
  ├─atd -f
  ├─avahi-daemon
  │   └─avahi-daemon
  ├─cgmanager -m name=systemd
  ├─colord
  │   ├─{gdbus}
  │   └─{gmain}
  ├─console-kit-dae --no-daemon
  │   ├─62*[{console-kit-dae}]
  │   ├─{gdbus}
  │   └─{gmain}
  ├─cron -f
  ├─cups-browsed
  │   ├─{gdbus}
  │   └─{gmain}
  ├─cupsd -l
  │   └─dbus dbus:// 
  ├─dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
  ├─dnsmasq -u lxc-dnsmasq --strict-order --bind-interfaces --pid-file=/run/lxc/dnsmasq.pid --listen-address 10.0.3.1 --dhcp-range 10.0.3.2,10.0.3.254 --dhcp-lease-max=253 --dhcp-no-override --except-interface=lo --interface=lxcbr0 --dhcp-leasefile=/var/lib/misc/dnsmasq.lxcbr0.leases --dhcp-authoritative
  ├─gdm
  │   ├─gdm-session-wor
  │   │   ├─gdm-x-session /usr/bin/gnome-session --autostart /usr/share/gdm/greeter/autostart
  │   │   │   ├─Xorg vt7 -displayfd 3 -auth /run/user/120/gdm/Xauthority -nolisten tcp -background none -noreset -keeptty -verbose 3
  │   │   │   │   ├─{llvmpipe-0}
  │   │   │   │   ├─{llvmpipe-1}
  │   │   │   │   └─{llvmpipe-2}
  │   │   │   ├─dbus-daemon --print-address 4 --session
  │   │   │   ├─gnome-session-b --autostart /usr/share/gdm/greeter/autostart
  │   │   │   │   ├─gnome-settings-
  │   │   │   │   │   ├─{dconf worker}
  │   │   │   │   │   ├─{gdbus}
  │   │   │   │   │   ├─{gmain}
  │   │   │   │   │   └─{pool}
  │   │   │   │   ├─gnome-shell --mode=gdm
  │   │   │   │   │   ├─ibus-daemon --xim --panel disable
  │   │   │   │   │   │   ├─ibus-dconf
  │   │   │   │   │   │   │   ├─{dconf worker}
  │   │   │   │   │   │   │   ├─{gdbus}
  │   │   │   │   │   │   │   └─{gmain}
  │   │   │   │   │   │   ├─ibus-engine-sim
  │   │   │   │   │   │   │   ├─{gdbus}
  │   │   │   │   │   │   │   └─{gmain}
  │   │   │   │   │   │   ├─{gdbus}
  │   │   │   │   │   │   └─{gmain}
  │   │   │   │   │   ├─{JS GC Helper}
  │   │   │   │   │   ├─{JS Sour~ Thread}
  │   │   │   │   │   ├─{Sync}
  │   │   │   │   │   ├─{dconf worker}
  │   │   │   │   │   ├─{gdbus}
  │   │   │   │   │   ├─{gmain}
  │   │   │   │   │   ├─{llvmpipe-0}
  │   │   │   │   │   ├─{llvmpipe-1}
  │   │   │   │   │   ├─{llvmpipe-2}
  │   │   │   │   │   └─{threaded-ml}
  │   │   │   │   ├─{dconf worker}
  │   │   │   │   ├─{gdbus}
  │   │   │   │   └─{gmain}
  │   │   │   ├─{gdbus}
  │   │   │   └─{gmain}
  │   │   ├─{gdbus}
  │   │   └─{gmain}

[…]
A.B.
  • 89,123
  • 21
  • 245
  • 323
1

I found a bashy way to achieve this from here

sudo ps aux | rev | cut -d ' ' -f 1 | rev

rev reverses the output of ps and then cut can help you out delimiting the first field(which is the last!) and then reverse it again...

worked for me.

Andrew
  • 274
  • 1
  • 3
  • 17
  • 3
    Processes with a space in their name won't be listed correctly by this method: `ps aux` itself will be listed as `aux`. – kos Nov 18 '15 at 07:07
  • 1
    `sudo` isn't necessary for `ps` and the command crops a lot of the commands, so it's not very accurate, if you would like to work with colums , use `awk` it's much better for that task. In fact, the exact command would be `ps aux | awk '{ for(i=1;i<=NF;i++) {if ( i >= 11 ) printf $i" "}; printf "\n" }'` – Sergiy Kolodyazhnyy Nov 18 '15 at 07:09
  • @kos yeah, I just realised it, thanks for pointing it out, Serg's `awk` should work it. – Andrew Nov 18 '15 at 07:20
  • @Serg +1, OP had requested `sudo ps` and I thought `awk` would be too complicated for this task and came out with `cut`. Anyways, shouldn't you update your answer with that code so the OP can accept it? – Andrew Nov 18 '15 at 07:21
  • It's better, but technically usernames on Linux can contain spaces, which could lead that command to mess up as well. `ps` it's just not blindly parsable. I think the only safe method here is `ps -e -o`. – kos Nov 18 '15 at 07:25
  • Yes, I probably should add that code. As for `sudo` , OP apparently also didn't know it's unnecessary. I'll edit my answer in a minute – Sergiy Kolodyazhnyy Nov 18 '15 at 07:26
  • @kos username wouldn't matter right? Oh you mean in the cases where processes are files a user is working on stored in his `/home/username`. So now my answer is now way useful. Easy is not always the answer :P – Andrew Nov 18 '15 at 07:38
  • No, I mean that if an username is "foo bar" all the following fields are shifted to the right by 1 field, hence field 11 starts a field before and catches the TIME column as well. – kos Nov 18 '15 at 07:55
  • In that case the user should change it to 12, that would solve the problem right? – Andrew Nov 18 '15 at 08:06