Skip to content

Instantly share code, notes, and snippets.

@omaryoussef
Last active March 1, 2022 15:00
Show Gist options
  • Save omaryoussef/4db45c8d7ae3770b1843b013f45a8aea to your computer and use it in GitHub Desktop.
Save omaryoussef/4db45c8d7ae3770b1843b013f45a8aea to your computer and use it in GitHub Desktop.
Tail multiple files with Tmux
#!/bin/bash
# By mapio
# https://github.com/mapio/tmux-tail-f/blob/master/tmux-tail-f
TMUX=$(type -p tmux) || { echo "This script requires tmux"; exit 1; }
SESSION="$TMUX-tail-f-$$"
NOKILL=0
LAYOUT=even-vertical
while getopts ":UHvhtd" opt; do
case $opt in
d) NOKILL=1;;
v) LAYOUT=even-vertical;;
h) LAYOUT=even-horizontal;;
t) LAYOUT=tiled;;
H) cat <<EOF
Usage: tmux-tail-f [OPTION]... [FILE]...
Tails the FILEs in a tmux window, one per pane; hitting `^C` in
any one of the panes will terminate all the tail commands and
kill the tmux session. Uses the `-F` option instead of `-f`,
which keeps trying to open a file if it is inaccessible.
Optional arguments:
-d do not kill tmux session upon detach.
-v use the 'even-vertical' layout (default).
-h use the 'even-horizontal' layout.
-t use the 'tiled' layout.
-H print this help.
EOF
exit
;;
*) echo "tmux: invalid option -- '$OPTARG', use -H for help" >&2; exit 1;;
esac
done
shift $(( OPTIND - 1 ))
[[ "$1" == "--" ]] && shift
function at_exit() {
$TMUX kill-session -t "$SESSION" >/dev/null 2>&1
}
[[ "$NOKILL" == "1" ]] || trap at_exit EXIT
$TMUX -q new-session -d -s "$SESSION"
for FILE in "$@"; do
$TMUX -q split-window -t "$SESSION" "tail -F '$FILE'"
$TMUX -q select-layout -t "$SESSION" tiled
done
$TMUX -q kill-pane -t "${SESSION}.0"
$TMUX -q select-pane -t "${SESSION}.0"
$TMUX -q select-layout -t "$SESSION" "$LAYOUT"
$TMUX -q set-window-option -t "$SESSION" synchronize-panes on
$TMUX -q attach -t "$SESSION" >/dev/null 2>&1
@kezzine
Copy link

kezzine commented Mar 1, 2022

love it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment