Page 1 of 1

CentOS/Fedora: /etc/bashrc tricks

Posted: 2023 May 03, 18:48
by Mihai
This template can be applied to other Linux operating systems

Code: Select all

PS1='\[\033[04;37m\]\u@\[\033[02;36m\]\h\[\033[03;31m\] Work: \[\033[02;32m\]\w \[\033[02;33m\] >\[\033[00m\] '
# Color definitions (taken from Color Bash Prompt HowTo).
# Some colors might look different of some terminals.
# For example, I see 'Bold Red' as 'orange' on my screen,
# hence the 'Green' 'BRed' 'Red' sequence I often use in my prompt.
# Normal Colors
Black='\e[0;30m'        # Black
Red='\e[0;31m'          # Red
Green='\e[0;32m'        # Green
Yellow='\e[0;33m'       # Yellow
Blue='\e[0;34m'         # Blue
Purple='\e[0;35m'       # Purple
Cyan='\e[0;36m'         # Cyan
White='\e[0;37m'        # White
# Bold
BBlack='\e[1;30m'       # Black
BRed='\e[1;31m'         # Red
BGreen='\e[1;32m'       # Green
BYellow='\e[1;33m'      # Yellow
BBlue='\e[1;34m'        # Blue
BPurple='\e[1;35m'      # Purple
BCyan='\e[1;36m'        # Cyan
BWhite='\e[1;37m'       # White
# Background
On_Black='\e[40m'       # Black
On_Red='\e[41m'         # Red
On_Green='\e[42m'       # Green
On_Yellow='\e[43m'      # Yellow
On_Blue='\e[44m'        # Blue
On_Purple='\e[45m'      # Purple
On_Cyan='\e[46m'        # Cyan
On_White='\e[47m'       # White
NC="\e[m"               # Color Reset
ALERT=${BWhite}${On_Red} # Bold White on red background

echo -e "${BCyan}This is BASH ${BRed}${BASH_VERSION%.*}${BCyan} - DISPLAY on ${BRed}$DISPLAY${NC}"
echo -e "\n${BYellow}Server date/time and zone is:$NC" ; date
# User promt configuration
export PROMPT_COMMAND="echo -n \[\$(date +%H:%M:%S)\]\ "
export HISTTIMEFORMAT="%d/%m/%y %T "
export HISTFILESIZE=100000
export HISTSIZE=100000
# Verificare conexiune internet
alias google='ping google.ro -c 3 -i 0.2'
# Custom "cd .."
alias sus='cd ..'
alias 2sus='cd ../../'
alias 3sus='cd ../../../'
alias 4sus='cd ../../../../'
alias back='cd $OLDPWD'
# Fedora/Centos/RHel Install
alias install='sudo yum install'
alias instup='sudo yum update -y'
alias instupg='sudo yum upgrade'
# list folders by size in current directory
alias du="du -h --max-depth=1 | sort -rh"
# Other aliases
alias vi="vim"
alias trace='mtr --report-wide --curses $1'
alias reload='source ~/.bashrc'
alias lasl='ls -asl'
## Dir shortcuts
alias home='cd ~/'
alias documents='cd ~/Documents'
alias downloads='cd ~/Downloads'
alias images='cd ~/Images'
alias videos='cd ~/Videos'
alias localhost='cd /var/www'
# Creates an archive from given directory
mktar() { tar cvf  "${1%%/}.tar"     "${1%%/}/"; }
mktgz() { tar cvzf "${1%%/}.tar.gz"  "${1%%/}/"; }
mktbz() { tar cvjf "${1%%/}.tar.bz2" "${1%%/}/"; }
# CPU Loaders
NCPU=$(grep -c 'processor' /proc/cpuinfo)    # Number of CPUs
SLOAD=$(( 100*${NCPU} ))        # Small load
MLOAD=$(( 200*${NCPU} ))        # Medium load
XLOAD=$(( 400*${NCPU} ))        # Xlarge load
# Returns system load as percentage, i.e., '40' rather than '0.40)'.
function load()
{
    local SYSLOAD=$(cut -d " " -f1 /proc/loadavg | tr -d '.')
    # System load of the current host.
    echo $((10#$SYSLOAD))       # Convert to decimal.
}
#-------------------------------------------------------------
# Process/system related functions:
#-------------------------------------------------------------

function my_ps() { ps $@ -u $USER -o pid,%cpu,%mem,bsdtime,command ; }
function pp() { my_ps f | awk '!/awk/ && $0~var' var=${1:-".*"} ; }
function killps()   # kill by process name
{
    local pid pname sig="-TERM"   # default signal
    if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
        echo "Usage: killps [-SIGNAL] pattern"
        return;
    fi
    if [ $# = 2 ]; then sig=$1 ; fi
    for pid in $(my_ps| awk '!/awk/ && $0~pat { print $1 }' pat=${!#} )
    do
        pname=$(my_ps | awk '$1~var { print $5 }' var=$pid )
        if ask "Kill process $pid <$pname> with signal $sig?"
            then kill $sig $pid
        fi
    done
}
function mydf()         # Pretty-print of 'df' output.
{                       # Inspired by 'dfc' utility.
    for fs ; do
        if [ ! -d $fs ]
        then
          echo -e $fs" :No such file or directory" ; continue
        fi
        local info=( $(command df -P $fs | awk 'END{ print $2,$3,$5 }') )
        local free=( $(command df -Pkh $fs | awk 'END{ print $4 }') )
        local nbstars=$(( 20 * ${info[1]} / ${info[0]} ))
        local out="["
        for ((j=0;j<20;j++)); do
            if [ ${j} -lt ${nbstars} ]; then
               out=$out"*"
            else
               out=$out"-"
            fi
        done
        out=${info[2]}" "$out"] ("$free" free on "$fs")"
        echo -e $out
    done
}
function ii()   # Get current host related info.
{
#    echo -e "\nYou are logged on ${BRed}$HOST"
    echo -e "\n${BRed}Additionnal information:$NC " ; uname -a
    echo -e "\n${BRed}Users logged on:$NC " ; w -hs |
             cut -d " " -f1 | sort | uniq
    echo -e "\n${BRed}Current date:$NC " ; date
    echo -e "\n${BRed}Machine stats:$NC " ; uptime
    echo -e "\n${BRed}Memory stats:$NC " ; free
    echo -e "\n${BRed}Diskspace:$NC " ; mydf / $HOME
    echo -e "\n${BRed}Open connections:$NC "; netstat -pan --inet
    echo -e "\n${BRed}iptables rulles:$NC "; iptables -vnL --line-numbers; iptables -vnL -t nat --line-numbers
    echo
}
# Print this on login or new terminal
    echo -e "${Cyan}"; cal -3
    echo -e "\n${BRed}Additionnal information:$NC " ; uname -a
    echo -e "\n${BRed}Users logged on:$NC " ; w -hs |
             cut -d " " -f1 | sort | uniq
    echo -e "\n${BRed}Machine stats:$NC " ; uptime
    echo -e "\n${BRed}Memory stats (MB):$NC " ; free -m
    echo -e "\n${BRed}Diskspace:$NC " ; mydf / $HOME
    echo
Make the bashrc settings permanent

Code: Select all

$ source ~/.bashrc
$ history
  226  26/06/15 18:23:09 cat /etc/bashrc
  227  26/06/15 18:23:21 vim /etc/bashrc
  228  26/06/15 18:38:31 source ~/.bashrc
  229  26/06/15 18:38:44 history