Search code examples
bashgrepxargs

Close tmux session with a particular name


I would like to have a command that closes all tmux session which name corresponds to a pattern. The pattern, in the language of regex, is nn\d+

I feel that I should be able to use a combination of grep and xargs.

I tried with tmux ls | grep -P "nn\d+" | .. but I am not sure how to use xargs here, that is: I am not sure how to refer to only the name part of the matched string, and to one string at the time.

To be more precise, the output of tmux ls is something like:

1: 1 windows (created Thu Mar 25 12:49:17 2021)
2: 1 windows (created Thu Mar 25 12:50:20 2021)
nn312133: 1 windows (created Thu Mar 25 12:53:54 2021)
nn3123: 1 windows (created Thu Mar 25 12:53:52 2021)

The output from grep is:

nn312133: 1 windows (created Thu Mar 25 12:53:54 2021)
nn3123: 1 windows (created Thu Mar 25 12:53:52 2021)

I should need to pipe nn312133 and nn3123 into tmux kill-session -t $x

Any idea? Thanks


Solution

  • The answer is tmux ls | grep -Po "nn\d+" | xargs -n1 tmux kill-session -t