我想按内存对所有进程进行排序
me@host:~$ ps au | sort -r -k 5 | head -10 | sed "s/$USER/me/g"
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
me 2312 0.0 0.1 958148 5568 tty2 Sl+ Mar11 0:00 /usr/lib/evolution/evolution-data-server/evolution-alarm-notify
me 2232 0.0 0.2 957248 7912 tty2 Sl+ Mar11 0:01 /usr/lib/gnome-settings-daemon/gsd-media-keys
me 2292 0.0 0.3 949556 11924 tty2 Sl+ Mar11 0:15 nautilus-desktop
me 3308 0.0 0.0 918708 2596 pts/0 Sl Mar11 0:04 /home/me/.lantern/bin/lantern
me 4728 0.1 0.5 872048 21648 tty2 Sl+ Mar11 1:00 gnome-control-center
me 2223 0.0 0.2 833712 8656 tty2 Sl+ Mar11 0:04 /usr/lib/gnome-settings-daemon/gsd-color
me 14638 0.0 0.2 83136 10400 pts/0 S+ 13:25 0:00 aria2c https://repo.anaconda.com/archive/Anaconda3-2018.12-MacOSX-x86_64.pkg
me 1799 1.8 1.2 745516 49592 tty2 Sl+ Mar11 17:36 /usr/lib/xorg/Xorg vt2 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -background none -noreset -keeptty -verbose 3
me 3587 0.0 0.4 736908 16952 tty2 Sl+ Mar11 0:32 /opt/google/chrome/chrome --type=renderer --field-trial-handle=9889395698754715859,17788752790984658481,131072 --service-pipe-token=121279454012343526 --lang=en-US --enable-crash-reporter=41f7a813-8595-4f48-8ba9-5b4493af761c, --enable-offline-auto-reload --enable-offline-auto-reload-visible-only --num-raster-threads=2 --enable-main-frame-before-activation --service-request-channel-token=121279454012343526 --renderer-client-id=22 --no-v8-untrusted-code-mitigations --shared-files=v8_context_snapshot_data:100,v8_natives_data:101
观察第5个字段,83136判断大于745515
我添加'--numeric-sort'
me@host:~$ ps au | sort -rn -k 5 | head -10 | sed "s/$USER/me/g"
me 2048 1.3 3.6 3324476 143932 tty2 Sl+ Mar11 12:30 /usr/bin/gnome-shell
me 3323 0.7 5.3 2110972 210872 pts/0 Sl Mar11 6:54 /home/me/.lantern/bin/lantern
me 3217 0.1 3.1 1834724 124076 tty2 Sl+ Mar11 1:02 /opt/google/chrome/chrome --type=renderer --field-trial-handle=9889395698754715859,17788752790984658481,131072 --service-pipe-token=14274241015953851945 --lang=en-US --enable-crash-reporter=41f7a813-8595-4f48-8ba9-5b4493af761c, --extension-process --enable-offline-auto-reload --enable-offline-auto-reload-visible-only --num-raster-threads=2 --enable-main-frame-before-activation --service-request-channel-token=14274241015953851945 --renderer-client-id=5 --no-v8-untrusted-code-mitigations --shared-files=v8_context_snapshot_data:100,v8_natives_data:101
me 2932 1.0 4.3 1463616 172764 tty2 SLl+ Mar11 9:54 /opt/google/chrome/chrome
me 2700 0.0 2.3 1263084 93984 tty2 SLl+ Mar11 0:14 /usr/bin/gnome-software --gapplication-service
me 2312 0.0 0.1 958148 5568 tty2 Sl+ Mar11 0:00 /usr/lib/evolution/evolution-data-server/evolution-alarm-notify
me 2232 0.0 0.1 957248 7632 tty2 Sl+ Mar11 0:01 /usr/lib/gnome-settings-daemon/gsd-media-keys
me 2292 0.0 0.6 949556 26132 tty2 Sl+ Mar11 0:15 nautilus-desktop
me 3308 0.0 0.0 918708 2584 pts/0 Sl Mar11 0:04 /home/me/.lantern/bin/lantern
me 4728 0.1 0.5 872048 21588 tty2 Sl+ Mar11 1:00 gnome-control-center
但是队长的描述消失了
我怎样才能对输出进行排序但立即让第一行带头?
'ps' 命令支持原生排序。您无需借助管道将其传递给正在删除标题的单独“排序”命令。
要对常驻内存进行排序,
或者对于虚拟内存,
我相信第二个选项将与您想要实现的目标相匹配。
你可以做
ps au | (read -r; printf "%s\n" "$REPLY"; sort -r --numeric-sort -k 5)| head -10 | sed "s/$USER/me/g
你可以在这里找到更多答案