How to tail multiple files using tail -0f in Linux/AIX

tail

I tried tailing two files using the option:

tail -0f file1.log -0f file2.log

In Linux I see an error "tail : can process only one file at a time".

In AIX I see the error as "Invalid options".

This works fine when I use:

tail -f file1 -f file 2

in Linux but not in AIX.

I want to be able to tail multiple files using -0f or -f in AIX/Linux

multitail is not recognized in either of these OS.

Best Answer

What about:

tail -f file1 & tail -f file2

Or prefixing each line with the name of the file:

tail -f file1 | sed 's/^/file1: /' &
tail -f file2 | sed 's/^/file2: /'

To follow all the files whose name match a pattern, you could implement the tail -f (which reads from the file every second continuously) with a zsh script like:

#! /bin/zsh -
zmodload zsh/stat
zmodload zsh/zselect
zmodload zsh/system
set -o extendedglob

typeset -A tracked
typeset -F SECONDS=0

pattern=${1?}; shift

drain() {
  while sysread -s 65536 -i $1 -o 1; do
    continue
  done
}

for ((t = 1; ; t++)); do
  typeset -A still_there
  still_there=()
  for file in $^@/$~pattern(#q-.NoN); do
    stat -H stat -- $file || continue
    inode=$stat[device]:$stat[inode]
    if
      (($+tracked[$inode])) ||
        { exec {fd}< $file && tracked[$inode]=$fd; }
    then
      still_there[$inode]=
    fi
  done
  for inode fd in ${(kv)tracked}; do
    drain $fd
    if ! (($+still_there[$inode])); then
      exec {fd}<&-
      unset "tracked[$inode]"
    fi
  done
  ((t <= SECONDS)) || zselect -t $((((t - SECONDS) * 100) | 0))
done

Then for instance, to follow all the text files in the current directory recursively:

that-script '**/*.txt' .
Related Question