A simple command to follow-tail a file’s changes with ansi color styling.

I wanted to do something with the cool regular expression support in Bash. Of course the code could be shorter by putting the filename in $1 and the optional number of lines in $2, but where is the fun in that.

  1. Dependencies
  2. The script
  3. Usage
  4. Related stuff

Dependencies

Make sure you have colorizer grc installed:

sudo apt install grc

And add ~/bin to your PATH, if it isn’t already:

mkdir -p ~/bin
echo 'PATH=$HOME/bin:$PATH' >> ~/.bashrc

The script

nano ~/bin/follow
#!/bin/bash

lines=0
file=$1

if [[ $1 =~ ^[0-9]+$ ]]; then
  lines=$1
  file=$2
fi

sudo grc tail -Fn $lines $file
chmod 0750 ~/bin/follow

Usage

$ follow [lines] file

Just track the system messages

follow /var/log/syslog

First print the last 50 lines and then follow

follow 50 /var/log/syslog

Follow a whole directory (w/o recursion)

follow /var/log/nginx/*
Categories: Code

Leave a Reply

Your email address will not be published. Required fields are marked *