Search code examples
bashtail

Bash:Tail logs without junk characters


Running tail -f /var/log/* can sometimes show junk/garbage characters and trash the screen with control codes.

What's a good way to filter those out, in order to see a clean output with minimal loss of information?


Solution

    1. Pipe to sed, to strip out ANSI codes (those likely to trash the console).
    2. Pipe to strings, to only print actual strings, because some files in /var/log/ contains binary data.

    tail -f /var/log/* | sed $'s#\e[\[(][[:digit:]]*[[:print:]]##g' | strings

    You can add these helper aliases to your shell profile by executing this:

    cat >>~/.profile <<EOF
    
    # ANSI codes stripping helpers
    alias noansi="sed $'s#\e[\[(][[:digit:]]*[[:print:]]##g'"
    alias noansistrings="noansi|strings"
    EOF
    . ~/.profile
    

    Then for example, you will be able to run:

    tail -f /var/log/* | noansistrings