Answers:
如果要删除所有分离的会话,可以使用以下代码:
tmux list-sessions | grep -E -v '\(attached\)$' | while IFS='\n' read line; do
tmux kill-session -t "${line%%:*}"
done
此解决方案比abieler提出的解决方案更健壮,因为它grep -E -v '\(attached\)$'
仅匹配分离的会话(abieler的解决方案将跳过称为attached的分离的会话)。
如果您想杀死所有独立的会话
tmux list-sessions | grep -v attached | cut -d: -f1 | xargs -t -n1 tmux kill-session -t
带有注释/解释:
tmux list-sessions | # list all tmux sessions
grep -v attached | # grep for all lines that do NOT contain the pattern "attached"
cut -d: -f1 | # cut with the separator ":" and select field 1 (the session name)
xargs -t -n1 ` # -t echoes the command, -n1 limits xargs to 1 argument ` \
tmux kill-session -t # kill session with target -t passed from xargs
-v
标志)。