Category: Tech articles

  • Colored Bash man pages and Log files

    \"colored_bash\"

    Below you can find bash codes which you can use with man pages or while reading log files:

    \'\\e[0;30m\' # Black - Regular
    \'\\e[0;31m\' # Red
    \'\\e[0;32m\' # Green
    \'\\e[0;33m\' # Yellow
    \'\\e[0;34m\' # Blue
    \'\\e[0;35m\' # Purple
    \'\\e[0;36m\' # Cyan
    \'\\e[0;37m\' # White
    
    \'\\e[1;30m\' # Black - Bold
    \'\\e[1;31m\' # Red
    \'\\e[1;32m\' # Green
    \'\\e[1;33m\' # Yellow
    \'\\e[1;34m\' # Blue
    \'\\e[1;35m\' # Purple
    \'\\e[1;36m\' # Cyan
    \'\\e[1;37m\' # White
    
    \'\\e[4;30m\' # Black - Underline
    \'\\e[4;31m\' # Red
    \'\\e[4;32m\' # Green
    \'\\e[4;33m\' # Yellow
    \'\\e[4;34m\' # Blue
    \'\\e[4;35m\' # Purple
    \'\\e[4;36m\' # Cyan
    \'\\e[4;37m\' # White
    
    \'\\e[40m\'   # Black - Background
    \'\\e[41m\'   # Red
    \'\\e[42m\'   # Green
    \'\\e[43m\'   # Yellow
    \'\\e[44m\'   # Blue
    \'\\e[45m\'   # Purple
    \'\\e[46m\'   # Cyan
    \'\\e[47m\'   # White
    \'\\e[0m\'    # Text Reset
    

    If you are using less application for viewing man pages you can add below variables to your .bashrc file. After that reload .bashrc variables with command: source /home/user/.bashrc and you will see colored man pages.

    export LESS_TERMCAP_mb=$\'\\E[01;31m\'
    export LESS_TERMCAP_md=$\'\\E[01;31m\'
    export LESS_TERMCAP_me=$\'\\E[0m\'
    export LESS_TERMCAP_se=$\'\\E[0m\'
    export LESS_TERMCAP_so=$\'\\E[01;44;33m\'
    export LESS_TERMCAP_ue=$\'\\E[0m\'
    export LESS_TERMCAP_us=$\'\\E[01;32m\'
    

    For log files use perl syntax with your combined regex:

     tail -f  /var/log/mail.log | perl -pe \'s/(fatal|error|panic|success)/\\e[1;31m$&\\e[0m/g\'
    

    Last colored trick. Use grep command with –color switch.