Last active
August 6, 2026 08:27
-
-
Save Thomashrb/94380faefb00251fe7be41a77a470fde to your computer and use it in GitHub Desktop.
Create named tmux sessions that starts in certain directories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| ## given the following starting point | |
| # tree -L 1 | |
| # . | |
| # ├── foo | |
| # ├── bar | |
| # └── tmux_sessions.sh | |
| # 2 directories, 1 files | |
| ## create this state, where each sessions 'root' is in the respective directories | |
| # tmux ls | |
| # foo: 1 windows (created Thu Aug 6 07:07:02 2026) | |
| # bar: 1 windows (created Thu Aug 6 07:04:07 2026) | |
| SESSIONNAMES=( | |
| "foo" | |
| "bar" | |
| ) | |
| for SESSIONNAME in "${SESSIONNAMES[@]}"; do | |
| SESSIONDIR="$(pwd)/$SESSIONNAME" | |
| # tmux has-session does not support exact match to use grep instead | |
| if tmux list-sessions -F '#{session_name}' | grep -Fx "$SESSIONNAME"; then | |
| echo "exists" | |
| else | |
| tmux new-session \ | |
| -s "$SESSIONNAME" \ | |
| -n script \ | |
| -c "$SESSIONDIR" \ | |
| -d | |
| fi | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment