Output only the column under COMMAND of `ps` command in terminal
Solution 1:
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
Solution 2:
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}
[…]