Bash Aliases FTW

When working on the CLI (command line interface) or, as it is widely known, "the prompt", most people prefer to set certain shortcuts as typing the same thing over and over is not something anyone enjoys. In Linux, such shortcuts are called aliases. This is a continuation from choosing and then .

bash_shellAn alias substitutes what is to the right of the equal sign, enclosed in single quotes, for what is to the left (and is usually typed at the prompt). In the Bourne Again Shell (bash), an alias cannot pass arguments inside, though it can in other shells. (Here we’re not considering the Korn or C shells.) If arguments need to be passed inside, one needs to define a function. When issuing the “alias” command, functions are not listed, so if you want it to appear in the least, consider further “aliasing” the function. Shell functions are also faster, being looked up before aliases.

Most people keep their aliases in a file called “.bash_aliases” which is called from .bashrc with

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

In the even that the distribution is updated and .bashrc modified, this allows the aliases to survive.

The following were collected from various sources; I honestly don’t remember where most are from, but I included a few links at the bottom. Note that most are suitable for the desktop, but may not be suitable for a server. Pick and choose as you see fit.

#Indelible Bonobo's ~/.bash_aliases
#don't forget sux; useradd native, adduser perl
alias reload='source $HOME/.bashrc'

#fc-list lists all fonts; .ttf in /usr/share/fonts
alias fontgrep='fc-list | grep'
alias fontsread='fc-cache -fv'
alias ports='netstat -tulanp'
alias wget='wget –c'

alias deltree='/bin/rm -R -f'
alias k9='kill -9'

#apt tool with sudo
alias apt-install='sudo apt-get install'
alias apt-search='apt-cache search'
alias apt-show='apt-cache show'
alias apt-purge='sudo apt-get --purge  remove'
alias apt-remove='sudo apt-get remove'
alias aptkey='sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys'

#more apt and dpkg shortcuts
alias upd8='sudo apt-get update && sudo apt-get dist-upgrade && sudo apt-get autoremove && sudo apt-get clean'
alias acm='apt-cache madison'
alias acp='apt-cache policy'
alias acs='apt-cache search'
alias aarc='auto-apt run ./configure'
alias dpkgrep='dpkg -l | grep -i'
alias dqp='dpkg-query -p'
alias dql='dpkg-query -L'
alias dqw='dpkg-query -W'
alias dqs='dpkg-query -S'
alias listfiles='dpkg-query -L'
alias findsource='dpkg-query -S'

#List including hidden files with indicator and color
alias ll='ls -al'
alias ls='ls -aF --color=always'

# Keep 1000 lines in .bash_history (default is 500)
export HISTSIZE=1000
export HISTFILESIZE=1000

# Set the default editor to nano (make sure is installed first).
export EDITOR=nano

# Change bash prompt to time+date+user+box+directory
export PS1='\d \@ \[\e[32;1m\]\u\[\e[34;1m\]@\[\e[36;1m\]\H \[\e[34;1m\]\w\[\e[32;1m\] $ \[\e[0m\]'

#temperatures - install acpi first
alias temps='acpi -t'
alias astats='acpi -V'

## pass options to free ##
alias meminfo='free -m -l -t'

#open last modified file in vim
alias Vim="vim `ls -t | head -1`"

#filesystem diskspace usage
alias dus='df -h'

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

## get top process eating memory
alias psmem='ps auxf | sort -nr -k 4'
alias psmem10='ps auxf | sort -nr -k 4 | head -10'

## get top process eating cpu ##
alias pscpu='ps auxf | sort -nr -k 3'
alias pscpu10='ps auxf | sort -nr -k 3 | head -10'

## Get server cpu info ##
alias cpuinfo='lscpu'

## older system use /proc/cpuinfo ##
alias cpuinfold='less /proc/cpuinfo' ##

## get GPU ram on desktop / laptop##
alias gpumeminfo='grep -i --color memory /var/log/Xorg.0.log'

## set some other defaults ##
alias df='df -H'
alias du='du -ch'

# Monitor logs
# alias syslog='sudo tail -100f /var/log/syslog'
# alias messages='sudo tail -100f /var/log/messages'

# List paths
alias echopath='echo -e ${PATH//:/\\n}'

# allows editing the path with each directory on a new line
nanopathd () 

    declare TFILE=/tmp/path.$LOGNAME.$$;
    echo $PATH | sed 's/^:/.:/;s/:$/:./' | sed 's/::/:.:/g' | tr ':' '\012' > $TFILE;
    nano $TFILE;
    PATH=`awk ' { if (NR>1) printf ":"
      printf "%s",$1 }' $TFILE`;
    rm -f $TFILE;
    echo $PATH

alias pathedit='nanopathd'

# perform 'ls' after 'cd' if successful.
cdls() {
  builtin cd "$*"
  RESULT=$?
  if [ "$RESULT" -eq 0 ]; then
    ls -al
  fi
}

# explode archive
ex () {
  if [ -f $1 ] ; then
      case $1 in
          *.tar.bz2)   tar xvjf $1    ;;
          *.tar.gz)    tar xvzf $1    ;;
          *.bz2)       bunzip2 $1     ;;
          *.rar)       unrar x $1     ;;
          *.gz)        gunzip $1      ;;
          *.tar)       tar xvf $1     ;;
          *.tbz2)      tar xvjf $1    ;;
          *.tgz)       tar xvzf $1    ;;
          *.zip)       unzip $1       ;;
          *.Z)         uncompress $1  ;;
          *.7z)        7z x $1        ;;
          *.exe)       cabextract $1  ;;
          *)           echo "'$1': unrecognized file compression" ;;
      esac
  else
      echo "'$1' is not a valid file"
  fi
}

# mount and unmount ISOs
miso () {
    [[ ! -f "$1" ]] && { echo "Provide a valid iso file"; return 1; }
    mountpoint="/media/${1//.iso}"
    sudo mkdir -p "$mountpoint"
    sudo mount -o loop "$1" "$mountpoint"

}
umiso () {
    mountpoint="/media/${1//.iso}"
    [[ ! -d "$mountpoint" ]] && { echo "Not a valid mount point"; return 1; }
    sudo umount "$mountpoint"
    sudo rm -ir "$mountpoint"

}

#mount NAS by changing private IP; install cifsutils +
# ..create .cred in root with username= password= and
# ..optionally domain= then chmod 600
mntnas () {
    umount /mnt/v1 && umount /mnt/v2
    mount -t cifs //192.168."$1"/Volume_1-1 /mnt/v1 -o user,uid=65001,rw,credentials=/root/.cred
    mount -t cifs //192.168."$1"/Volume_2-1 /mnt/v2 -o user,uid=65001,rw,credentials=/root/.cred
}

# find top 5 big files
alias findbig="find . -type f -exec ls -s {} \; | sort -n -r | head -5"

# Grep for a bash process
alias psg="ps -aux ¦ grep bash"

# pings
alias ping1='ping -c 4 www.google.com'
alias ping0='ping -c 4 192.168.0.1'

#find file
alias f='find . |grep '

#search history
alias h='history|grep '

#search processes
alias p='ps aux |grep '

#open any file or folder with default app
alias o='xdg-open '

#python calculator
alias pc='python -i -Qnew -c "from math import *"'

#python calculator (plus numpy and matplotlib)
alias pcn='python -i -Qnew -c "from math import *;from pylab import *"'

To temporarily use a command that is “aliased”, you have to escape it with “\”; e.g.,

$ \cp * /some/location

..will avoid using the alias version of the command (the ‘$’ symbolizes the prompt and is not part of the command). To disable an alias use unalias.

I have not tested and used all; if something does not work, please let me know.

Sources / More info: wiki-bash, uf-al, sample, bash

blog comments powered by Disqus
Your gift enables us to continue to provide high quality reporting with little or no advertising.