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 case "$1" in 61 -h|--h|-help|--help) 62 awk '/^# +psfzf /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 63 exit 0 64 ;; 65 66 --c|--cpu) sort="--sort=-%cpu,-rss"; shift; continue ;; 67 --i|--id|--pid) sort="--sort=pid"; shift; continue ;; 68 --m|--mem|--rss) sort="--sort=-rss,-%cpu"; shift; continue ;; 69 --u|--user) sort="--sort=user,-%cpu,-rss"; shift; continue ;; 70 71 -d|-docker|--docker|docker) 72 fetch="docker ${fetch}"; shift; continue 73 ;; 74 75 --p|--pm|-podman|--podman|podman) 76 fetch="podman ${fetch}"; shift; continue 77 ;; 78 79 --) shift; break ;; 80 -*) fetch="${fetch} $1"; shift; continue ;; 81 esac 82 83 fetch="${fetch} $1" && shift 84 done 85 86 if [ "${fetch}" = 'ps' ]; then 87 fetch='ps aux' 88 fi 89 90 when="$(date +'%Y-%m-%d %H:%M:%S')" 91 data="$(${fetch} ${sort})" 92 res=$? 93 [ "${res}" -ne 0 ] && exit "${res}" 94 95 keys='ctrl-a:select-all,ctrl-space:toggle,F10:abort,F12:abort' 96 pick="fzf --reverse -m --header-first --header-lines=1 --bind ${keys}" 97 header="$(echo "${data}" | head -n 1)" 98 title="$(printf "%-20s (run on %s)\n" "${fetch}" "${when}")" 99 data="$(echo "${data}" | grep '[^ ]')" 100 101 data="$(echo "${data}" | ${pick} --header="${title}" "$@")" 102 res=$? 103 [ "${res}" -ne 0 ] && exit "${res}" 104 105 printf "%s\n" "${header}" 106 echo "${data}" | awk '/[^ ]/'