Question

What are your favorite bash aliases?

Bash aliases are awesome. They can save you keystrokes and give you shortcuts to some of your most used commands. I’ll share some of my own down in the answers.

What’s in your .bashrc file? What bash aliases have you found the most useful?

Subscribe
Share

ew, nano

The publically shareable section of my bashrc file!

# Reloads the bashrc file
alias bashreload="source ~/.bashrc && echo Bash config reloaded"

# Open nano and make backup of original file. Useful for config files and things you don't want to edit the original
function nanobk() {
	echo "You are making a copy of $1 before you open it. Press enter to continue."
	read nul
	cp $1 $1.bak
	nano $1
}

# Clear DNS Cache

# Still need testing on this one

alias flushdns="sudo /etc/init.d/dns-clean restart && echo DNS cache flushed"


# Get IPs associated with this site

# Work to dynamically list all interfaces. Will add later. 
# Currently only uses the hardcoded interface names

function myip()
{
    extIp=$(dig +short myip.opendns.com @resolver1.opendns.com)

    printf "Wireless IP: "
    MY_IP=$(/sbin/ifconfig wlp4s0 | awk '/inet/ { print $2 } ' |
      sed -e s/addr://)
    echo ${MY_IP:-"Not connected"}


    printf "Wired IP: "
    MY_IP=$(/sbin/ifconfig enp0s25 | awk '/inet/ { print $2 } ' |
      sed -e s/addr://)
    echo ${MY_IP:-"Not connected"}

    echo ""
    echo "WAN IP: $extIp"

}


# Syntax: "repeat [X] [command]"
function repeat()      
{
    local i max
    max=$1; shift;
    for ((i=1; i <= max ; i++)); do  # --> C-like syntax
        eval "$@";
    done
}


# Make some of the file manipulation programs verbose
alias mv="mv -v"
alias rm="rm -vi"
alias cp="cp -v"

# Prints disk usage in human readable form
alias d="du -sh"

# Clear the screen of your clutter
alias c="clear"
alias cl="clear;ls;pwd"

# GREP Motifications
alias grep="grep --color"
alias grepp="grep -P --color"


# Json tools (pipe unformatted to these to nicely format the JSON)
alias json="python -m json.tool"
alias jsonf="python -m json.tool"

# Edit shortcuts for config files
alias sshconfig="${EDITOR:-nano} ~/.ssh/config"
alias bashrc="${EDITOR:-nano} +120 ~/.bashrc && source ~/.bashrc && echo Bash config edited and reloaded."

# SSH helper
alias sshclear="rm ~/.ssh/multiplex/* -f && echo SSH connection cache cleared;"
alias sshlist="echo Currently open ssh connections && echo && l ~/.ssh/multiplex/"

I only use one, apt to alias aptitude …

Love the json one. Can’t never remember the name of the library!

although actually, the d alias looks handy


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

alias myips="ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'" shows a listing of your IP addresses, works great if you have multiple /dev/eth* (OSX/nix)

My favorite

alias fuck='sudo !!'

Some functions inside my .zshrc

function clock() {
    watch -t -n1 "echo "" && date +""$'  '"%T""
}
function c() {
    clear; cd $1; l;
}

(for mac users)

# Change directory to the current Finder directory
function cdf() {
    target=`osascript -e 'tell application "Finder" to if (count of Finder windows) > 0 then get POSIX path of (target of front Finder window as text)'`
    if [ "$target" != "" ]; then
        cd "$target"; pwd
    else
        echo 'No Finder window found' >&2
    fi
}

One I must have when I use Sublime Text

alias slt='subl'

When I do presentations on my Mac I like to hide the mess on my desktop.

alias hideDesktop='defaults write com.apple.finder CreateDesktop -bool false; killall Finder;'
alias showDesktop='defaults write com.apple.finder CreateDesktop -bool true; killall Finder;'

Start my screensaver

alias afk="open /System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/ScreenSaverEngine.app"

Send my Mac to sleep:

alias sleep="pmset sleep now"

List global installed packages and bottles.

alias npml='npm list -g --depth=0'
alias brewl='brew leaves'

Some of my favorites:

Merges current branch into master then git checkout back to develop

alias mergemaster='git checkout master; git pull; git merge develop; git push; git checkout develop'

Adds everything from current folder to git

alias add='git add .'

For Node developers:

alias s='npm start'
alias lint='npm run lint'

Docker:

alias dockerstop='docker-compose stop'
alias dockerup='docker-compose up -d'
alias dockerrm='docker-compose rm --all'

In last few months I started using ZSH + http://ohmyz.sh/ as my default shell and it’s AWESOME!

My favorite alias/function:

randompw()
{
    cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w ${1:-32} | head -n 1
}

Keep all your bash history:

export HISTTIMEFORMAT="%s "
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND ; }"'echo $$ $USER \
               "$(history 1)" >> ~/.bash_eternal_history'

extract files:

extract () {
  if [ -f $1 ] ; then
    case $1 in
      *.tar.bz2)  tar xjf $1    ;;
      *.tar.gz) tar xzf $1    ;;
      *.bz2)    bunzip2 $1    ;;
      *.rar)    rar x $1    ;;
      *.gz)   gunzip $1   ;;
      *.tar)    tar xf $1   ;;
      *.tbz2)   tar xjf $1    ;;
      *.tgz)    tar xzf $1    ;;
      *.zip)    unzip $1    ;;
      *.Z)    uncompress $1 ;;
      *)      echo "'$1' cannot be extracted via extract()" ;;
    esac
  else
    echo "'$1' is not a valid file"
  fi
}

Complete hosts:

_complete_hosts () {
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    host_list=`{
        for c in /etc/ssh_config /etc/ssh/ssh_config ~/.ssh/config
        do [ -r $c ] && sed -n -e 's/^Host[[:space:]]//p' -e 's/^[[:space:]]*HostName[[:space:]]//p' $c
        done
        for k in /etc/ssh_known_hosts /etc/ssh/ssh_known_hosts ~/.ssh/known_hosts
        do [ -r $k ] && egrep -v '^[#\[]' $k|cut -f 1 -d ' '|sed -e 's/[,:].*//g'
        done
        sed -n -e 's/^[0-9][0-9\.]*//p' /etc/hosts; }|tr ' ' '\n'|grep -v '*'`
    COMPREPLY=( $(compgen -W "${host_list}" -- $cur))
    return 0
}
complete -F _complete_hosts ssh
complete -F _complete_hosts sshs
complete -F _complete_hosts host
complete -F _complete_hosts telnet
complete -F _complete_hosts ping

Complete lxc/lxd containers: (~/etc/containers must be created first)

_complete_container () {
    lxc list|grep PERSIS | awk '{print $2}'|sort|uniq >| /home/$USER/etc/containers
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    host_list=`{
        for k in /home/jonathan/etc/containers
        do [ -r $k ] && egrep -v '^[#\[]' $k|cut -f 1 -d ' '|sed -e 's/[,:].*//g'
        done
        sed -n -e 's/^[0-9][0-9\.]*//p' /home/jonathan/etc/containers; }|tr ' ' '\n'|grep -v '*'`
    COMPREPLY=( $(compgen -W "${host_list}" -- $cur))
    return 0
}
complete -F _complete_container c

c() {
        lxc exec $1 -- /bin/bash
}

proxy remote:

proxy_src_remote () {
 ssh $1@$2 -R 3128:localhost:3128
}

A docker and git alias, mainly because I can’t type and my typos are actually quite comical in terms of their use/misuse.

alias dicker='docker'
alias goit='git'

A few docker shortcuts.

# Docker stuff
# Clean stopped containers
alias cco="docker ps -a | grep "Exited" | awk '{print $1}' | xargs docker rm"
# After that, clean unused images to reclaim disk space
alias cimg="docker images | grep none | awk '{print \$3}' | xargs docker rmi"
# Finally the regular stuff just to save typing.
alias dps="docker ps -a"
alias di="docker images"
alias drmi="docker rmi"
alias drm="docker rm"

A system notification to popup when a long shell task finishes:

# Display an "alert" after running commands.  Use like so:
#   sleep 10; alert "Done sleeping"
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$//'\'')"'

Ha ha I have this one:

alias PANIC="espeak 'OH NO, the sky is falling'"

Everyone in the office just rolls their eyes

Here are a few more of my favorite aliases.

Show/hide hidden files in the macOS Finder:

alias show="defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder"
alias hide="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder"

Limit ping to five ECHO_REQUEST packets:

alias ping='ping -c 5'

Display drives and space in human readable format:

alias drives='df -h'

Run this one in ~/ to discover what folders are using the most space:

alias usage='du -h -d1'

Colorize grep output

alias grep='grep --color=auto -n'

Clear the screen (can’t live without this one!)

alias c='clear'

That’s all, folks!