How to store the contents of every tmux pane

Store contents of every tmux pane using shell script.

Shell script

Use the following shell script to capture the data. Inspect it to learn how to target each tmux session, window an pane.

#!/bin/bash
# Store the contents of every tmux pane
#

# Get timestamp
timestamp="$(date +%s)"

# Get home dir
tmux_home_directory="$HOME"

# Define output directory
output_directory="${tmux_home_directory}/.tmux/logs/$(hostname -s)-${timestamp}"

# Create output directory
mkdir -p ${output_directory}

# Display output directory
echo "Output directory: ${output_directory}"

# Display date
date | tee "${output_directory}/date"

# Iterate over tmux sessions
tmux_sessions="$(tmux list-sessions -F "#{session_id}")"
for tmux_session in $tmux_sessions; do

  tmux_session_name="$(tmux display-message -t "${tmux_session}" -p "#{session_name}")"
  echo "Session: ${tmux_session_name} (${tmux_session})" | tee -a "${output_directory}/overview"

  # Iterate over tmux windows
  tmux_windows="$(tmux list-windows -F "#{window_id}" -t "${tmux_session}")"
  for tmux_window in $tmux_windows; do
    tmux_window_name="$(tmux display-message -t "${tmux_session}:${tmux_window}" -p "#{window_name}")"
    echo "  Window: ${tmux_window_name} (${tmux_window})" | tee -a "${output_directory}/overview"

    # Iterate over tmux panes
    tmux_panes="$(tmux list-panes -F "#{pane_id}" -t "${tmux_session}:${tmux_window}")"
    for tmux_pane in $tmux_panes; do
      tmux_pane_name="$(tmux display-message -c "${tmux_session}:${tmux_window}" -t "${tmux_pane}" -p "#{pane_title}")"
      echo "    Pane: ${tmux_pane_name} (${tmux_pane})" | tee -a "${output_directory}/overview"

      tmux capture-pane -t "${tmux_pane}" -b temp-capture-buffer-${timestamp} -S -
      tmux save-buffer -b temp-capture-buffer-${timestamp} "${output_directory}/${tmux_session}-${tmux_window}-${tmux_pane}.log"
      tmux delete-buffer -b capture-buffer-${timestamp}
    done
  done
done

Usage

Execute the above-mentioned shell script.

$ tmux.sh
Output directory: /home/milosz/.tmux/logs/milosz-XPS-13-9343-1562702696
Tue Jul  9 22:04:56 CEST 2019
Session: 2 ($2)
  Window: bash (@3)
    Pane: milosz-XPS-13-9343 (%11)
    Pane: milosz-XPS-13-9343 (%12)
Session: 3 ($3)
  Window: bash (@4)
    Pane: milosz-XPS-13-9343 (%14)
    Pane: milosz-XPS-13-9343 (%17)
    Pane: mc [[email protected]]:~/Projekty/blog/assets (%15)
  Window: bash (@5)
    Pane: milosz-XPS-13-9343 (%16)
Session: 4 ($4)
  Window: bash (@7)
    Pane: milosz-XPS-13-9343 (%20)
Session: 8 ($8)
  Window: htop (@12)
    Pane: milosz-XPS-13-9343 (%29)
  Window: bash (@13)
    Pane: milosz-XPS-13-9343 (%30)
Session: home ($0)
  Window: vim (@0)
    Pane: milosz-XPS-13-9343 (%2)
    Pane: milosz-XPS-13-9343 (%6)
    Pane: milosz-XPS-13-9343 (%19)
    Pane: milosz-XPS-13-9343 (%8)
  Window: bash (@1)
    Pane: milosz-XPS-13-9343 (%7)
  Window: bash (@6)
    Pane: milosz-XPS-13-9343 (%18)
Session: work ($1)
  Window: bash (@8)
    Pane: milosz-XPS-13-9343 (%24)

Inspect created directory.

$ ll /home/milosz/.tmux/logs/milosz-XPS-13-9343-1562702696
total 988
-rw-rw-r-- 1 milosz milosz  41121 Jul  9 22:04 '[email protected]%19.log'
-rw-rw-r-- 1 milosz milosz  47072 Jul  9 22:04 '[email protected]%2.log'
-rw-rw-r-- 1 milosz milosz  42727 Jul  9 22:04 '[email protected]%6.log'
-rw-rw-r-- 1 milosz milosz  38352 Jul  9 22:04 '[email protected]%8.log'
-rw-rw-r-- 1 milosz milosz  10063 Jul  9 22:04 '[email protected]%7.log'
-rw-rw-r-- 1 milosz milosz  86257 Jul  9 22:04 '[email protected]%18.log'
-rw-rw-r-- 1 milosz milosz   4813 Jul  9 22:04 '[email protected]%24.log'
-rw-rw-r-- 1 milosz milosz  81861 Jul  9 22:04 '[email protected]%11.log'
-rw-rw-r-- 1 milosz milosz 144384 Jul  9 22:04 '[email protected]%12.log'
-rw-rw-r-- 1 milosz milosz 114458 Jul  9 22:04 '[email protected]%14.log'
-rw-rw-r-- 1 milosz milosz 141742 Jul  9 22:04 '[email protected]%15.log'
-rw-rw-r-- 1 milosz milosz 175881 Jul  9 22:04 '[email protected]%17.log'
-rw-rw-r-- 1 milosz milosz    142 Jul  9 22:04 '[email protected]%16.log'
-rw-rw-r-- 1 milosz milosz     95 Jul  9 22:04 '[email protected]%20.log'
-rw-rw-r-- 1 milosz milosz  17789 Jul  9 22:04 '[email protected]%29.log'
-rw-rw-r-- 1 milosz milosz  17066 Jul  9 22:04 '[email protected]%30.log'
-rw-rw-r-- 1 milosz milosz     30 Jul  9 22:04  date
-rw-rw-r-- 1 milosz milosz    894 Jul  9 22:04  overview

Inspect date.

$ cat /home/milosz/.tmux/logs/milosz-XPS-13-9343-1562702696/date
Tue Jul  9 22:04:56 CEST 2019

Inspect tmux overview.

$ cat /home/milosz/.tmux/logs/milosz-XPS-13-9343-1562702696/overview
Session: 2 ($2)
  Window: bash (@3)
    Pane: milosz-XPS-13-9343 (%11)
    Pane: milosz-XPS-13-9343 (%12)
Session: 3 ($3)
  Window: bash (@4)
    Pane: milosz-XPS-13-9343 (%14)
    Pane: milosz-XPS-13-9343 (%17)
    Pane: mc [[email protected]]:~/Projekty/blog/assets (%15)
  Window: bash (@5)
    Pane: milosz-XPS-13-9343 (%16)
Session: 4 ($4)
  Window: bash (@7)
    Pane: milosz-XPS-13-9343 (%20)
Session: 8 ($8)
  Window: htop (@12)
    Pane: milosz-XPS-13-9343 (%29)
  Window: bash (@13)
    Pane: milosz-XPS-13-9343 (%30)
Session: home ($0)
  Window: vim (@0)
    Pane: milosz-XPS-13-9343 (%2)
    Pane: milosz-XPS-13-9343 (%6)
    Pane: milosz-XPS-13-9343 (%19)
    Pane: milosz-XPS-13-9343 (%8)
  Window: bash (@1)
    Pane: milosz-XPS-13-9343 (%7)
  Window: bash (@6)
    Pane: milosz-XPS-13-9343 (%18)
Session: work ($1)
  Window: bash (@8)
    Pane: milosz-XPS-13-9343 (%24)

Inspect specific pane contents.

$ tail /home/milosz/.tmux/logs/milosz-XPS-13-9343-1562702696/\[email protected]%1*
==> /home/milosz/.tmux/logs/milosz-XPS-13-9343-1562702696/[email protected]%11.log <==
"unconfined") (using servicehelper)
Jul  3 00:02:23 milosz-XPS-13-9343 dbus-daemon[1230]: [system] Successfully activated service 'org.kde.powerdevil.backlighthelper'
Jul  3 00:03:19 milosz-XPS-13-9343 dbus-daemon[1230]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service' requested by ':1.11' (uid=0 pid=1476 comm="/usr/sbin/NetworkManager --no-daemon " label="unconfined")
Jul  3 00:03:19 milosz-XPS-13-9343 systemd[1]: Starting Network Manager Script Dispatcher Service...
Jul  3 00:03:19 milosz-XPS-13-9343 dbus-daemon[1230]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'

==> /home/milosz/.tmux/logs/milosz-XPS-13-9343-1562702696/[email protected]%12.log <==
19567 -------->      Process 19566 is writing to it using its 1
19567 -------->      I am reading from it
19567 --------> File descriptor 1 points to pipe:[23699554]
19567 -------->      I am writing to it
19567 -------->      Process 19568 is reading it using its fd 0
19568 ----------> CMD: grep system
19568 ----------> File descriptor 0 points to pipe:[23699554]
19568 ---------->      Process 19567 is writing to it using its 1
19568 ---------->      I am reading from it

This is really cool!