New .bash_aliases file

One really handy thing you can do in your Ubuntu installation is to add a custom .bash_aliases file.Some background…
  • When you SSH into your server a number of commands and files are run. One of these is the .bashrc file which lives in the user’s home directory. This is custom for each user. So when I log in as root (see this post) the file /root/.bashrc is executed.
  • .bashrc calls .bash_aliases (in the same directory) which is then also executed.
  • Typically .bash_aliases contains various alias commands which map one command onto another. This means that you can for example map ll to ls -al. Overall this can make life easier to run common commands quickly.
  • While it may not be best practice, you can actually add anything to your .bash_aliases file to be run on logon. The reason why I add more than just aliases to this file is that I re-use the same .bash_aliases file in various installations (my NAS, my Raspberry Pi, …) and on all of these there are a few common startup tasks I want to run. I don’t want to have to add these to crontab manually on each installation and some of my installs are performed very frequently (e.g. the Raspberry Pi). So just maintaining one common .bash_aliases file is much easier.
Here is an extract from my .bash_aliases file. Feel free to copy any or all of this. There are also some links at the bottom of this post with great posts on a few forums where people have described what they put in their .bash_aliases file.
Note that if you make changes to your .bash_aliases file, you can reload it without logging out and in by running
source ~/.bashrc
#######################
# ##### COLOURS ##### #
#######################
#Set up terminal to output 256 colours
export TERM=xterm-256color

#Some functions to output colors
black() { echo "$(tput setaf 0)$*$(tput setaf 9)"; }
red() { echo "$(tput setaf 1)$*$(tput setaf 9)"; }
green() { echo "$(tput setaf 2)$*$(tput setaf 9)"; }
yellow() { echo "$(tput setaf 3)$*$(tput setaf 9)"; }
blue() { echo "$(tput setaf 4)$*$(tput setaf 9)"; }
magenta() { echo "$(tput setaf 5)$*$(tput setaf 9)"; }
cyan() { echo "$(tput setaf 6)$*$(tput setaf 9)"; }
white() { echo "$(tput setaf 7)$*$(tput setaf 9)"; }

# Define a few Colours
BLACK='e[0;30m'
BLUE='e[0;34m'
GREEN='e[0;32m'
CYAN='e[0;36m'
RED='e[0;31m'
PURPLE='e[0;35m'
BROWN='e[0;33m'
LIGHTGRAY='e[0;37m'
DARKGRAY='e[1;30m'
LIGHTBLUE='e[1;34m'
LIGHTGREEN='e[1;32m'
LIGHTCYAN='e[1;36m'
LIGHTRED='e[1;31m'
LIGHTPURPLE='e[1;35m'
YELLOW='e[1;33m'
WHITE='e[1;37m'
NC='e[0m'              # No Color


#####################################
# ##### ENVIRONMENT VARIABLES ##### #
#####################################
declare -x HISTFILE=~/.bash_history
declare -x HISTCONTROL=ignoredups
declare -x HISTFILESIZE=100000
declare -x HISTSIZE=100000


###############
### aliases ###
###############
#Contains bash aliases for quickly calling bash commands
alias h='history'
alias dir='dir --color=auto'
alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
alias n='sudo nano'

# listings
alias ls='ls --color=auto'
alias ll='ls -lah'                # human readable (sizes) long and all ;-)
alias lls='ls -l -h -g -F --color=auto'
alias lc='ls -aCF'
alias lsam='ls -am'               # List files horizontally
alias lr='ls -lR'                 # recursive
alias lsx='ls -ax'                # sort right to left rather then in columns
alias lss='ls -shAxSr'            # sort by size
alias lt='ls -lAtrh'              # sort by date and human readable
alias lm='ls -al |more'           # pipe through 'more'

# Directory navigation aliases
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'

########################
###  EXPORTS & PATHS ###
########################
# Set some environment variables
  export SETUP_PATH="/root/RaspPi/Setup"
  export DESKTOP="/root/Desktop"
  export RTHOME="/root"

# Set another PATH destination for ease of use
  PATH=$PATH:/home/ryan/scripts/suspend_on_idle/:/home/ryan/scripts/Torrenting/:/home/ryan/scripts/Torrenting/Filebot


#################
### FUNCTIONS ###
#################
#netinfo - shows network information for your system
netinfo () {
  echo "--------------- Network Information ---------------"
  /sbin/ifconfig | awk /'inet addr/ {print $2}'
  /sbin/ifconfig | awk /'Bcast/ {print $3}'
  /sbin/ifconfig | awk /'inet addr/ {print $4}'
  /sbin/ifconfig | awk /'HWaddr/ {print $4,$5}'
  myip=`lynx -dump -hiddenlinks=ignore -nolist http://checkip.dyndns.org:8245/ | sed '/^$/d; s/^[ ]*//g; s/[ ]*$//g' `
  echo "${myip}"
  echo "---------------------------------------------------"
}

#Grep for processes matching $1...
psgrep() {
 if [ ! -z $1 ] ; then
  echo "Grepping for processes matching $1..."
  ps aux | grep $1 | grep -v grep
 else
  echo "!! Need name to grep for"
 fi
}

# clock - A bash clock that can run in your terminal window.
clock ()
{
while true;do clear;echo "===========";date +"%r";echo "===========";sleep 1;done
}
#To allow individual repo updates, use  e.g. if repo in sources.d in file kodi.list: update_repo kodi
update_repo() {
    sudo apt-get update -o Dir::Etc::sourcelist="sources.list.d/$1.list" 
        -o Dir::Etc::sourceparts="-" -o APT::Get::List-Cleanup="0"
}


#######################################################
# WELCOME SCREEN (with calendar)
#######################################################
clear
echo -ne "${LIGHTGREEN}" "Hello, $USER. today is, "; date
echo -e "${WHITE}"; cal ; 
echo -ne "${CYAN}";
echo -ne "${LIGHTPURPLE}Sysinfo:";uptime ;echo ""


############################## ##################################
# ##### COMMAND PROMPT SECTION #############################
############################## ##################################
#Uncomment the one you want
#PS1="[[32m]u:w > [[39m]"
#PS1="[[31m][[[32m]u[[31m]][[32m]w > [[39m]"
#PS1="[[40;1;31m][[[1;32m]u[[1;31m]][[49;1;32m]w > [[22;39m]"
#PS1="[[1;31m][[[1;32m][[47m]u[[1;31m][[49m]][[1;32m]w > [[22;39m]"
#PS1="[[31m][[[36m]u[[31m]][[36m]w > [[39m]"
#PS1="[[32m]u [[39m]$[[32m] w [[39m]"
Useful sources

Leave a Reply

(email optional)