Useful Bash Aliases

August 17, 2019

When it comes to working on the command line, we all want to be as efficient as possible and make life more manageable, whether we are doing a git status or just trying to ping an IP address.

If you need some convincing check out: Do you need bash aliases? Or, if you are new to aliases, check out: How to Set Up Bash Aliases

Below we have provided many of the bash aliases we use daily and will update this list as we find more.


## Directory listings
alias la="ls -lA"                  # Long listing format
alias ll="ls -alF"                 #
alias lt="ls -lAt | head -21"      #
alias lr="ls -lAtr | head -21"     #
alias las="ls -lAS"                #
alias lc="ls | wc -l"              #

## Directory changing
alias cd..='cd ..'                 # Prevent command not found
alias ..='cd ..'                   #
alias ...='cd ../..'               #
alias ....='cd ../../..'           #
alias .....='cd ../../..'          #
alias ......='cd ../../../..'      #
alias .......='cd ../../../../..'  #
alias .2='cd ../..'                #
alias .3='cd ../../..'             #
alias .4='cd ../../../..'          #
alias .5='cd ../../../../..'       #
alias .6='cd ../../../../../..'    #
alias .7='cd ../../../../../../..' #

alias cd1='cd $(dirNum 1)'
alias cd2='cd $(dirNum 2)'
alias cd3='cd $(dirNum 3)'
alias cd4='cd $(dirNum 4)'

alias cat1='cat $(fileNum 1)'
alias cat2='cat $(fileNum 2)'
alias cat3='cat $(fileNum 3)'
alias cat4='cat $(fileNum 4)'

alias less1='less -S $(fileNum 1)'
alias less2='less -S $(fileNum 2)'
alias less3='less -S $(fileNum 3)'
alias less4='less -S $(fileNum 4)'

# Get the nth lastest directory by name
function dirNum() { ls -lt | grep ^d | sed "$1q;d" | awk '{print $9}'; };
function fileNum() { ls -lt | grep ^- | sed "$1q;d" | awk '{print $9}'; };


# Git Aliases
## General
alias gs="clear; git status"
alias ga="git add --all"
alias gp="git pull --all -p"
alias gas="ga; gs"
alias gc="git commit -m"
alias gcl="git config --list"
alias grs="git remote set-url origin"
alias gpa="git push -u origin --all && git push -u origin --tags"
## Branch
alias gb="git branch -a"
## Stash
alias gsl="git stash list"
## Submodule
alias gsi="git submodule init"
alias gsu="git submodule update"

## Delete all local branches that do not exist in the repo
function gclean() {
  git fetch --all -p && git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -d
function gdclean() {
  git fetch --all -p && git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D


## General
alias sites = "cd ~/Sites"       # MacOS Specific

## Services
alias dirapa="cd /etc/apache2"   # Apache
alias dirngi="cd /etc/nginx"     # Nginx
alias dirmys="cd /etc/mysql"     # MySQL


# Docker Aliases
## Debian/Ubuntu based systems using bash
function deb() { docker exec -it $1 /bin/bash; };
## Alpine based systems using ash
function dea() { docker exec -it $1 /bin/ash; };

alias dil="docker image ls"
alias dcl="docker container ls"
alias dnl="docker network ls"
alias dvl="docker volume ls"

alias dup="docker-compose up"
alias dupd="docker-compose up -d"
alias dub="docker-compose up --build"
alias dubd="docker-compose up --build -d"
alias dcd="docker-compose down"

alias dpu='docker system prune --all --force --volumes'
## View docker logs, append container name when using
alias dl='docker logs'
## Warning: Prunes everything from your system, do NOT run on production
alias dpa='docker stop $(docker container ls -a -q) && docker system prune -a -f --volumes'


In this example, we have used batman as our demo users home directory.

# SSH Aliases
export SSH_RSA="/Users/batman/.ssh/id_rsa"
export SSH_USER="root"
export SSH_CERT="/Users/batman/.ssh/certificates"

## SSH Key Copy
alias sshcp="ssh-copy-id -i ~/.ssh/"

## AWS
function ssm() {
  aws ssm start-session --target $1

## DevOps
  # GitLab
  alias dogit="ssh -i $SSH_RSA $SSH_USER@ip-address"
  alias doruna="ssh -i $SSH_RSA $SSH_USER@ip-address"
  alias dorunb="ssh <user>@<ip-address>"


# Laravel Aliases
## Artisan
alias migrate="clear; php artisan migrate -v"
alias fresh="clear; php artisan migrate:fresh -v"
alias refresh="clear; php artisan migrate:refresh -v"
alias rollback="clear; php artisan migrate:rollback -v"
alias seed="refresh --seed"
alias arl="php artisan route:list"
alias aclear="php artisan route:clear; php artisan view:clear; php artisan cache:clear"
alias serve="clear; php artisan serve"
alias dusk="clear; php artisan dusk"
alias tink="clear; php artisan tinker" # Poor little tink tink

## Vendor
alias phpunit="./vendor/bin/phpunit"
alias phpcs="./vendor/bin/phpcs --standard=<ruleset.xml> --encoding=utf-8 -np"
alias phpcsi="vendor/bin/phpcs --config-set installed_paths $PWD/vendor/escapestudios/symfony2-coding-standard"

## Composer Aliases
alias codu="composer dump-autoload"
alias coin="composer install"
alias coup="composer update"
## This is convienent when you are running composer in docker but your IDE needs a vendor folder locally 
alias codock="composer install --ignore-platform-reqs --no-scripts"


## Checking size
alias df="df -H"                   #
alias du="du -h"                   #
alias dc="du -ch"                  #
alias ds="du -sh"                  #

## System info & memory
alias meminfo='free -m -l -t'                                   # Pass options to free
alias psmem='ps auxf | sort -nr -k 4'                           # Get processes eating memory
alias psmem10='ps auxf | sort -nr -k 4 | head -10'              # Get top processes eating memory
alias pscpu='ps auxf | sort -nr -k 3'                           # Get processes eating cpu
alias pscpu10='ps auxf | sort -nr -k 3 | head -10'              # Get top processes eating cpu
alias cpuinfo='lscpu'                                           # Get server cpu info
alias gpumeminfo='grep -i --color memory /var/log/Xorg.0.log'   # Get GPU ram on desktop / laptop

## iptable view & edit
alias ipt='sudo /sbin/iptables'
alias iptlist='sudo /sbin/iptables -L -n -v --line-numbers'
alias iptlistin='sudo /sbin/iptables -L INPUT -n -v --line-numbers'
alias iptlistout='sudo /sbin/iptables -L OUTPUT -n -v --line-numbers'
alias iptlistfw='sudo /sbin/iptables -L FORWARD -n -v --line-numbers'
alias firewall=iptlist

# Stop after sending count ECHO_REQUEST packets #
alias ping='ping -c 3'

## Windows CMD Conversions
alias cls="clear"                  # Clear the screen
alias c="clear"                    # Clear the screen
alias cl="clear; la"               # Clear the screen
alias ipconfig="ifconfig"          # Show network interface parameters

## Searching
alias fd="find . -type d -name"      # Find directory by name
alias ff="find . -type f -name"      # Find file by name

function hg() { history | grep "$1"; };


For those on Mac OSX with a TouchBar and who are frequently on multiple boxes/servers or debugging docker containers, check out BetterTouchTool. We love this software for its ability to customize the TouchBar. After seeing -bash: la: command not found so many times, we installed BetterTouchTool and added an Aliases touch button that appears when in a Terminal window. After we SSH onto a remote server, we hit the Aliases button, and all the aliases we know and love are now set up for the current session. Note: BetterTouchTool is not free, but it is very reasonably priced and well worth it.


If there is an alias we missed or one you can not live without, let us know.