Skip to content

Instantly share code, notes, and snippets.

@airglow923
Last active December 18, 2020 08:28
Show Gist options
  • Save airglow923/246f06c681a2cbe6042a287d3de2be11 to your computer and use it in GitHub Desktop.
Save airglow923/246f06c681a2cbe6042a287d3de2be11 to your computer and use it in GitHub Desktop.
Shell commands to kill detached (unattached) tmux sessions
#!/bin/bash
TMUX_UNATTACHED=$(tmux ls | grep -E -v '\(attached\)$')
while read session; do
tmux kill-session -t "${line%%:*}"
done <<< $TMUX_UNATTACHED
@airglow923
Copy link
Author

airglow923 commented Dec 18, 2020

Alternatively, append the following lines to the system shell configuration file (i.e..~/.bashrc, ~/.zshrc, etc.):

tmux-kill-unattached() {
  TMUX_UNATTACHED=$(tmux ls | grep -E -v '\(attached\)$')

  while read session; do
    tmux kill-session -t "${line%%:*}"
  done <<< $TMUX_UNATTACHED
}

and run source ~/.bashrc to refresh the current terminal session. Change .bashrc if you are using different shell config files.

Then, enter tmux-kill-unattached to execute the function.

This way is preferred since the first method requires users to specify the path to the tmux-kill-unattached.sh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment