Bash tricks

January 1, 2016 at 12:00am in bash
by Roy Willemse (1 min read)

This is a collection of snippets that help improve my command line experience. Most of this stuff is loaded in my ~/.bashrc file.

Version control your scripts

My scripts live in ~/scripts, which is a Git repo. I then simply source them into .bashrc

source ~/scripts/alias.bashrc
source ~/scripts/fun.bashrc

Define aliases to simplify common tasks

Directory navigation

alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'

Git commands

alias gs='git status'
alias gl='git log --graph --oneline'
alias gc='git commit'

Functions for more power

Create and change to a directory at once

function mcdir() {
    if [[ "$1" == "" ]]; then 
        echo -e "Create a directory and change to it.\nUsage: mcdir <directory>"
        return
    fi
    mkdir -p "$1" && cd "$1"
}