File: psfzf.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright (c) 2026 pacman64 6 # 7 # Permission is hereby granted, free of charge, to any person obtaining a copy 8 # of this software and associated documentation files (the "Software"), to deal 9 # in the Software without restriction, including without limitation the rights 10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 # copies of the Software, and to permit persons to whom the Software is 12 # furnished to do so, subject to the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be included in 15 # all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 # SOFTWARE. 24 25 26 # psfzf [extra options...] [ps options...] [--...] [fzf options...] 27 # 28 # PS + FZF lets you interactively filter a snapshot of all currently running 29 # processes, by running `ps` and piping it into the interactive picker `fzf`. 30 # 31 # The result is a subset of the rows the command `ps` would have given you. 32 # you can give `fzf` extra options by preceding them with a `--` (no quotes). 33 # 34 # Besides the usual `ps`-specific options, this script offers easier-to-use 35 # extra options 36 # 37 # --c reverse-sort entries by latest sampled CPU use 38 # --cpu reverse-sort entries by latest sampled CPU use 39 # 40 # --h show this help message 41 # --help show this help message 42 # 43 # --m reverse-sort entries by latest sampled RSS (memory) use 44 # --mem reverse-sort entries by latest sampled RSS (memory) use 45 # --rss reverse-sort entries by latest sampled RSS (memory) use 46 # 47 # --u sort/group entries by user 48 # --user sort/group entries by user 49 # 50 # -docker, --docker, docker 51 # run `docker ps` instead of `ps` 52 # 53 # --pm, -podman, -podman, --podman, podman 54 # run `podman ps` instead of `ps` 55 56 57 fetch='ps' 58 59 while [ $# -gt 0 ]; do 60 if [ "$1" = '--' ]; then 61 shift 62 break 63 fi 64 65 case "$1" in 66 -h|--h|-help|--help) 67 awk '/^# +psfzf /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 68 exit 0 69 ;; 70 71 --c|--cpu) 72 sort="--sort=-%cpu,-rss" 73 shift 74 continue 75 ;; 76 77 --i|--id|--pid) 78 sort="--sort=pid" 79 shift 80 continue 81 ;; 82 83 --m|--mem|--rss) 84 sort="--sort=-rss,-%cpu" 85 shift 86 continue 87 ;; 88 89 --u|--user) 90 sort="--sort=user,-%cpu,-rss" 91 shift 92 continue 93 ;; 94 95 -docker|--docker|docker) 96 fetch="docker ${fetch}" 97 shift 98 continue 99 ;; 100 101 --p|--pm|-podman|--podman|podman) 102 fetch="podman ${fetch}" 103 shift 104 continue 105 ;; 106 107 -*) 108 fetch="${fetch} $1" 109 shift 110 continue 111 ;; 112 esac 113 114 fetch="${fetch} $1" 115 shift 116 done 117 118 if [ "${fetch}" = 'ps' ]; then 119 fetch='ps aux' 120 fi 121 122 data="$(${fetch} ${sort})" 123 res=$? 124 if [ "${res}" -ne 0 ]; then 125 exit "${res}" 126 fi 127 128 keys='ctrl-a:select-all,ctrl-space:toggle' 129 pick="fzf --reverse -m --header-first --bind ${keys}" 130 header="$(echo "${data}" | awk 'NR == 1 { print; exit }')" 131 data="$(echo "${data}" | awk 'NR > 1 && /[^ ]/')" 132 133 data="$(echo "${data}" | ${pick} --header="${header}" "$@")" 134 res=$? 135 if [ "${res}" -ne 0 ]; then 136 exit "${res}" 137 fi 138 139 printf "%s\n" "${header}" 140 echo "${data}" | awk '/[^ ]/'