JosefJezek
2/24/2014 - 2:04 PM

Bash Aliases for Ubuntu

Bash Aliases for Ubuntu

# Bash Aliases for Ubuntu
# Install: curl https://gist.githubusercontent.com/josefjezek/9188977/raw -o ~/.bash_aliases

# Enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls -hF --color=auto'
    #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'
else
    alias ls='ls -hF'
fi


# Modified commands
alias diff='colordiff'              # requires colordiff package
alias free='free -mlt'
alias more='less'
alias df='df -hT'
alias du='du -c -h'
alias mkdir='mkdir -p -v'
alias nano='nano -w'
alias ping='ping -c 5'
alias ..='cd ..'

## ls
alias lr='ls -R'                    # recursive ls
alias ll='ls -l'
alias la='ll -A'
alias lx='ll -BX'                   # sort by extension
alias lz='ll -rS'                   # sort by size
alias lt='ll -rt'                   # sort by date
alias lm='la | more'
# Show hidden files only
alias l.='ls -d .* --color=auto'
# Recursive directory listing
alias lr='ls -R | grep ":$" | sed -e '\''s/:$//'\'' -e '\''s/[^-][^\/]*\//--/g'\'' -e '\''s/^/   /'\'' -e '\''s/-/|/'\'''


# New commands
alias da='date "+%A, %B %d, %Y [%T]"'
alias du1='du --max-depth=1'
alias historytop='history | awk "{ print $2 }" | sort | uniq -c | sort -rn | head'
alias openports='netstat --all --numeric --programs --inet --inet6'
alias pg='ps -Af | grep $1'         # requires an argument (note: /usr/bin/pg is installed by the util-linux package; maybe a different alias name should be used)

## Pipe content to and from the clipboard
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'


# Special commands
alias connections='sudo lsof -n -P -i +c 15'
alias arp-clear='ip -s -s neigh flush all'
alias wipe='shred -v'
# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'


# Privileged access
if [ $UID -ne 0 ]; then
    alias sudo='sudo '
    alias scat='sudo cat'
    alias svim='sudo vim'
    alias root='sudo su'
    alias reboot='sudo reboot'
    alias halt='sudo halt'
    alias update='sudo pacman -Su'
    alias netcfg='sudo netcfg2'
fi


# Safety features
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -I'                    # 'rm -i' prompts for every file
alias ln='ln -i'
alias chown='chown --preserve-root'
alias chmod='chmod --preserve-root'
alias chgrp='chgrp --preserve-root'


# Config files
alias sshc='vi ~/.ssh/config'


# Home subfolders
alias documents='cd ~/Documents'
alias downloads='cd ~/Downloads'
alias desktop='cd ~/Desktop'
alias music='cd ~/Music'
alias videos='cd ~/Videos'


# Package control
alias agi='sudo apt-get install'
alias agr='sudo apt-get remove'
alias agp='sudo apt-get purge'
alias agu='sudo apt-get update'
alias agfi='sudo apt-get -f install' # try to fix broken dependencies
alias acs='apt-cache search'
alias acsh='apt-cache show'
alias acp='apt-cache policy'
alias clean='sudo apt-get clean && sudo apt-get autoclean && sudo apt-get dist-clean && sudo apt-get autoremove'
alias upgrade='sudo apt-get update && sudo apt-get upgrade && sudo apt-get clean'
alias dpi='sudo dpkg -i'
alias dps='dpkg -S' # search for files in installed pkgs
alias dpc='dpkg -c' # list contents of a local deb-file
alias dpl='dpkg -l' # list installed pkgs
alias dplf='dpkg -L' # list files in one installed pkg, e.g. dplf sudoku | grep /usr/games/
alias dpr='sudo dpkg-reconfigure' # reconfigure an already installed package


# Python HTTP module serve the current directory to 0.0.0.0 and safety port 8765
# Detect python version
ret=`python -c 'import sys; print("%i" % (sys.hexversion<0x03000000))'`
if [ $ret -eq 0 ]; then
    # python version is >= 3
    alias serve='python -m http.server 8765 > /dev/null 2>&1 &'
else 
    # python version is < 3
    alias serve='python -m SimpleHTTPServer 8765 > /dev/null 2>&1 &'
fi