What are useful Bash aliases and functions

As a command line adventurer, you probably found yourself repeating the same lengthy commands over and over. If you always ssh into the same machine, if you always chain the same commands together, or if you constantly run a program with the same flags, you might want to save the precious seconds of your life that you spend repeating the same actions over and over.

The solution to achieve that is to use an alias. As you may know, an alias is a way to tell your shell to remember a particular command and give it a new name: an alias. However, an alias is quickly limited as it is just a shortcut for a shell command, without the ability to pass or control the arguments. So to complement, bash also allows you create your own functions, which can be more lengthy and complex, and also accepts any number of arguments.

Naturally, like with soup, when you have a good recipe you share it. So here is a list with some of the most useful bash aliases and functions. Note that "most useful" is loosely defined, and of course the usefulness of an alias is dependent on your everyday usage of the shell.

Before you start experimenting with aliases, here is a handy tip: if you give an alias the same name as a regular command, you can choose to launch the original command and ignore the alias with the trick:

\command

For example, the first alias below replaces the ls command. If you wish to use the regular ls command and not the alias, call it via:

\ls

Productivity

So these aliases are really simple and really short, but they are mostly based on the idea that if you save yourself a fraction of a second every time, it might end up accumulating years at the end. Or maybe not.

alias ls="ls --color=auto"

Simple but vital. Make the ls command output in color.

alias ll = "ls --color -al"

Shortcut to display in color all the files from a directory in a list format.

alias grep='grep --color=auto'

Similarly, put some color in the grep output.

mcd() { mkdir -p "$1"; cd "$1";} 

One of my favorite. Make a directory and cd into it in one command: mcd [name].

cls() { cd "$1"; ls;}

Similar to the previous function, cd into a directory and list its content: cls [name].

backup() { cp "$1"{,.bak};}

Simple way to make a backup of a file: backup [file] will create [file].bak in the same directory.

md5check() { md5sum "$1" | grep "$2";}

Because I hate comparing the md5sum of a file by hand, this function computes it and compares it using grep: md5check [file] [key].

alias makescript="fc -rnl | head -1 >" 

Easily make a script out of the last command you ran: makescript [script.sh]

alias genpasswd="strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo" 

Just to generate a strong password instantly.

alias c="clear"

Cannot do simpler to clean your terminal screen.

alias histg="history | grep"

To quickly search through your command history: histg [keyword]

alias ..='cd ..'

No need to write cd to go up a directory.

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

Similarly, go up two directories.

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

Longest but also the most useful. Extract any kind of archive: extract [archive file]

System Info

Want to know everything about your system as quickly as possible?

alias cmount="mount | column -t"

Format the output of mount into columns.

alias tree="ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'" 

Display the directory structure recursively in a tree format.

sbs() { du -b --max-depth 1 | sort -nr | perl -pe 's{([0-9]+)}{sprintf "%.1f%s", $1>=2**30? ($1/2**30, "G"): $1>=2**20? ($1/2**20, "M"): $1>=2**10? ($1/2**10, "K"): ($1, "")}e';} 

"Sort by size" to display in list the files in the current directory, sorted by their size on disk.

alias intercept="sudo strace -ff -e trace=write -e write=1,2 -p" 

Intercept the stdout and stderr of a process: intercept [some PID]. Note that you will need strace installed.

alias meminfo='free -m -l -t'

See how much memory you have left.

alias ps? = "ps aux | grep"

Easily find the PID of any process: ps? [name]

alias volume="amixer get Master | sed '1,4 d' | cut -d [ -f 2 | cut -d ] -f 1"

Displays the current sound volume.

Networking

For all the commands that involve the Internet or your local network, there are fancy aliases for them.

alias websiteget="wget --random-wait -r -p -e robots=off -U mozilla"

Download entirely a website: websiteget [URL]

alias listen="lsof -P -i -n" 

Show which applications are connecting to the network.

alias port='netstat -tulanp'

Show the active ports

gmail() { curl -u "$1" --silent "https://mail.google.com/mail/feed/atom" | sed -e 's/<\/fullcount.*/\n/' | sed -e 's/.*fullcount>//'}

Rough function to display the number of unread emails in your gmail: gmail [user name]

alias ipinfo="curl ifconfig.me && curl ifconfig.me/host"

Get your public IP address and host.

getlocation() { lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep 'city|state|country'|awk '{print $3,$4,$5,$6,$7,$8}'|sed 's\ip address flag \\'|sed 's\My\\';} 

Returns your current location based on your IP address.

Useless

So what if some aliases are not all that productive? They can still be fun.

kernelgraph() { lsmod | perl -e 'print "digraph \"lsmod\" {";<>;while(<>){@_=split/\s+/; print "\"$_[0]\" -> \"$_\"\n" for split/,/,$_[3]}print "}"' | dot -Tpng | display -;}

To draw the kernel module dependency graph. Requires image viewer.

alias busy="cat /dev/urandom | hexdump -C | grep "ca fe"" 

Make you look all busy and fancy in the eyes of non-technical people.

To conclude, a good chunk of these aliases and functions come from my personal .bashrc, and the awesome websites alias.sh and commandlinefu.com which I already presented in my post on the best online tools for Linux. So definitely go check them out, make your own recipes, and if you are so inclined, share your wisdom in the comments.

As a bonus, here is the plain text version of all the aliases and functions I mentioned, ready to be copy pasted in your bashrc.

#Productivity 
alias ls="ls --color=auto" 
alias ll="ls --color -al" 
alias grep='grep --color=auto' 
mcd() { mkdir -p "$1"; cd "$1";} 
cls() { cd "$1"; ls;} 
backup() { cp "$1"{,.bak};} 
md5check() { md5sum "$1" | grep "$2";} 
alias makescript="fc -rnl | head -1 >" 
alias genpasswd="strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo" 
alias c="clear" 
alias histg="history | grep" 
alias ..='cd ..' 
alias ...='cd ../..'
extract() { 
    if [ -f $1 ] ; then 
      case $1 in 
        *.tar.bz2)   tar xjf $1     ;; 
        *.tar.gz)    tar xzf $1     ;; 
        *.bz2)       bunzip2 $1     ;; 
        *.rar)       unrar e $1     ;; 
        *.gz)        gunzip $1      ;; 
        *.tar)       tar xf $1      ;; 
        *.tbz2)      tar xjf $1     ;; 
        *.tgz)       tar xzf $1     ;; 
        *.zip)       unzip $1       ;; 
        *.Z)         uncompress $1  ;; 
        *.7z)        7z x $1        ;; 
        *)     echo "'$1' cannot be extracted via extract()" ;; 
         esac 
     else 
         echo "'$1' is not a valid file" 
     fi 
} 
 
#System info 
alias cmount="mount | column -t" 
alias tree="ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'" 
sbs(){ du -b --max-depth 1 | sort -nr | perl -pe 's{([0-9]+)}{sprintf "%.1f%s", $1>=2**30? ($1/2**30, "G"): $1>=2**20? ($1/2**20, "M"): $1>=2**10? ($1/2**10, "K"): ($1, "")}e';} 
alias intercept="sudo strace -ff -e trace=write -e write=1,2 -p" 
alias meminfo='free -m -l -t' 
alias ps?="ps aux | grep" 
alias volume="amixer get Master | sed '1,4 d' | cut -d [ -f 2 | cut -d ] -f 1" 
 
#Network 
alias websiteget="wget --random-wait -r -p -e robots=off -U mozilla" 
alias listen="lsof -P -i -n" 
alias port='netstat -tulanp'
gmail() { curl -u "$1" --silent "https://mail.google.com/mail/feed/atom" | sed -e 's/<\/fullcount.*/\n/' | sed -e 's/.*fullcount>//'}
alias ipinfo="curl ifconfig.me && curl ifconfig.me/host" 
getlocation() { lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep 'city|state|country'|awk '{print $3,$4,$5,$6,$7,$8}'|sed 's\ip address flag \\'|sed 's\My\\';} 
 
#Funny 
kernelgraph() { lsmod | perl -e 'print "digraph \"lsmod\" {";<>;while(<>){@_=split/\s+/; print "\"$_[0]\" -> \"$_\"\n" for split/,/,$_[3]}print "}"' | dot -Tpng | display -;} 
alias busy="cat /dev/urandom | hexdump -C | grep \"ca fe\""

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.

Adrien Brochard

I am a Linux aficionado from France. After trying multiple distributions, I finally settled for Archlinux. But I am always trying to improve my system by stacking up tips and tricks.

Latest posts by Adrien Brochard (see all)

15 thoughts on “What are useful Bash aliases and functions

  1. Very great aliases, I wanted to use almost all of them. I had a problem with:

    sbs(){...}
    getlocation(){...}

    It seems like bash have a problem with {...} part.

  2. mcd(){mkdir -p "$1"; cd "$1";}
    bash: syntax error near unexpected token `{mkdir'
    the error was resolved when:
    space follows function name
    space follows 1st '{'
    thus:
    mcd () { mkdir -p "$1"; cd "$1";} // worked, but, why?
    os:
    linux mint 17

  3. Here are three that I use quite a bit:
    alias lsl="ls -l"
    alias lsla="ls -la"
    alias ping="ping -c4"

    Keeps me from having to reach for the '-' every time :).

  4. I like your aliases, but I use something slightly different to backup a file, which works better for me:

    savefile () {
    if [ -z "$1" ]; then
    echo "savefile needs a filename to save..."
    return
    fi
    local _da=`date +%Y%m%d_%H%M`
    until [ -z "$1" ]; do
    if [ -f "$1" ]; then
    /bin/cp -p $1 $1.${_da}
    echo "$1 saved as $1.${_da}"
    else
    echo "$1 is not a file"
    fi
    shift
    done
    }
    I have something similar to save a directory in a tgz.

  5. getlocation has a syntax error:

    justme@ispy:~$ exec bash
    bash: /home/justme/.bashrc: line 103: syntax error near unexpected token `{lynx'
    bash: /home/justme/.bashrc: line 103: `getlocation(){lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep 'city|state|country'|awk '{print $3,$4,$5,$6,$7,$8}'|sed 's\ip address flag \\'|sed 's\My\\';} '

  6. Here's the fix. (I don't know much about bash so correct me if this isn't right but it seems to work!)

    The following format gives a syntax error : mcd() { mkdir -p "$1"; cd "$1";}

    However, if you change it to look like this, indenting 4 spaces, it doesn't cause any problems anymore:
    mcd() {
    mkdir -p "$1"; cd "$1";
    }

  7. Here is a very convenient command I use frequently to create directories prefixed with the current date. The date is formated as YYYY-MM-DD to insure that the lexical order matches the creation order.

    mkdate() { local DIR="$(date +%F)-$1" ; mkdir "$DIR" && cd "$DIR" ; }

  8. I tried to put all of Adrien Brochard's extraordinary bash functions and aliases into my .bashrc file.
    Unfortunately, bash on Fedora 20
    bash.x86_64 4.2.53-1.fc20 @updates
    bash-completion.noarch 1:2.1-3.fc20 installed
    will not scan this file. Drops off the end with "unexpected EOF"
    Solution I found is to
    leave the functions in .bashrc
    move al the aliases into a ".bash_aliases" file in the home directory.
    Then, at the end of the .bashrc file, insert
    if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
    fi

    This works. I have tried all the functions and aliases, and all work except kernelgraph. Problem with my ImageMagik image viewer, I think.
    Thank you, Adrien, for some really useful things.
    Pat

  9. alias update='yum update'
    alias grep='grep --color'
    alias egrep='egrep --color'
    alias ipconfig='ifconfig'

    and very useful in .bashrc:
    export HISTSIZE=""
    export HISTIGNORE='&:[ ]*'

  10. Some of these are a bit dodgy, as if the author doesn't know standard commands? E.g., there's control-l to clear your screens, control-r to search your history, and tree is already a command. At best the majority of these are only saving you a couple of keystrokes while obfuscating some really valuable parameters to know.

Leave a comment

Your email address will not be published. Required fields are marked *