brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: tmux ls -F prints sessions the way a script wants them

Tmux
~/tips/tip-tmux-ls-format

tmux ls -F '#{session_name}: #{session_windows} windows'

Lists tmux sessions in a format you choose, for example only the name and window count.

The default tmux ls output is fine for reading but awkward to parse. The -F flag takes a format string with the same #{...} variables the status bar uses.

tmux ls -F '#{session_name}: #{session_windows} windows'

With two sessions called a and b that prints a: 1 windows and b: 1 windows, one per line, without the creation date.

Anything you can show in the status bar you can print here. #{session_name}, #{session_windows} and #{session_attached} are the useful ones for sessions. The full list sits under the FORMATS heading in the manual.

A small example for a script: tmux ls -F '#{session_name}' | grep -qx work tells you with its exit code whether a session called work exists. That is cleaner than parsing the default output with cut and sed, and it keeps working if tmux changes how it prints the date.

With no server running, tmux prints an error such as no server running instead of an empty list, so check the exit code in a script.

Try it without risk: start a throwaway session with tmux new -s scratch, use the shortcut or command above there, look at what changes, and close the session again with tmux kill-session -t scratch.

Further reading: tmux manual page (OpenBSD, official), tmux wiki: Getting Started (official), On this site: tmux new -As work attaches to a session or creates it, On this site: Ctrl-b $ renames the tmux session you are in

More Tmux tips tmux new -d -s report -x 200 -y 50 'top' tmux wait-for build-done set -g allow-passthrough on tmux kill-server Ctrl-b [ All Tmux tips (125) All topics

Link