Ubuntu: Creating an alias for common commands under bash

ubuntuIf you have Bash console commands that you type out frequently, consider creating an alias.  By simply adding a “.bash_aliases” files in your home directory, you can easily define shortcuts to frequent commands.

For example, if you frequently list all the files in a directory reverse sorted by date, you may end up typing “ls -latr” many times in a day.  Put this into the .bash_aliases file and source it.

$ echo 'alias lsr="ls -latr"' >> ~/.bash_aliases
$ . ~/.bash_aliases

Then typing the alias should give you a file list in reverse date order.

$ lsr

We forced the reload above, but in a normal login process the .bash_aliases file is loaded by ~/.bashrc.

If you have an alias command that requires a single quote, you must use single quotes as the outer characters and then escape each single quote inside using SQUOTE BACKSLASH SQUOTE SQUOTE.  The command below does a long file listing and uses awk to parse only the 5th column, which is size.

alias lss='ls -l | awk {'\''print $5'\''}'

 

REFERENCES

https://stackoverflow.com/questions/1250079/how-to-escape-single-quotes-within-single-quoted-strings