fzf is an awesome tool for filtering and selecting lists of things on the command line.
kill <tab>
In addition to the autocomplete for kill
, we can use fzf
to pick from other lists as well. We can roughly reconstruct
the behavior with a chain of other commands.
ps aux | fzf -m | awk '{print $2}' | xargs kill
Let's do something a bit more fun. We can boot up a kube cluster using GKE, then interactively select a pod to exec into. First let's create a cluster and start a sample pod.
gcloud container clusters create test-clustergcloud container clusters get-credentials test-clusterkubectl create -f https://k8s.io/docs/tasks/debug-application-cluster/shell-demo.yaml
We can check to make sure the pod is running by getting the pods.
➜ kubectl get podsNAME READY STATUS RESTARTS AGEshell-demo 1/1 Running 0 22m
Now, with fzf
, we can construct a command which will let
us interactively pick a pod to exec into.
kubectl get pods --no-headers \| fzf | awk '{print $1}' \| xargs -o -I % kubectl exec -it % bash
Pick the pod (of which there's only one for us right now).
This will get you a shell in a pod something like this, which you can then use and exit.
root@shell-demo:/# lsbin dev home lib64 mnt proc run srv tmp varboot etc lib media opt root sbin sys usrroot@shell-demo:/# exitexit
The xargs
arguments are pretty crucial here. -I %
allows
us to use %
in our command at any arbitrary point. %
will get replaced with the pod name.
If we didn't use -o
, kubectl
would error out complaining
about not having a tty.
-o Reopen stdin as /dev/tty in the child processbefore executing the command. This is usefulif you want xargs to run an interactive application.