How to create an alias in Ubuntu 16 via the commandline

Do you ever get tired of having to type out a long string of Linux commands and options/arguments repeatedly? Well, you can use aliases to either abbreviate those long commands, or simply change them to something that is easier to remember.The alias command does this by replacing of a default system command with a word/string of your choice.

Using aliases is very useful especially when there are default arguments that you always attach to a command. For example, i think it’s always a good idea to create a backup of a file before you edit it, in case something goes wrong and you have to revert to the original. So whenever i use nano (a text-based editor), i add the arguments -B (which backups the original file) and -u (which allows me to undo an edits). Normally, i would have to type out

nano -Bu

every time i edit a file, but that is somewhat laborious (trust me, when you do a lot of file editing, it gets tiring!). I’m also forgetful, so i don’t always remember to do it. Which is why I use aliases. Instead of typing out the nano command and its arguments every time i want to edit a file, i simply create an alias.

To create an alias in Ubuntu 16 via the commandline, do the following:

Open .bashrc

sudo nano -Bu ~/.bashrc

(options – B backups up the file, u is undo)

Scroll down to the bottom of the file and add the desired aliases

#Custom Aliases

alias nano='nano -Bu'  # backup original file and undo changes
alias rm='rm -i' # request confirmation before deleting a file

Save and close the file (Ctrl + O, Enter, then Ctrl + X)

To save the alias and make it permanent, execute the following command [note the space between the first period and the tilde (~) ]
. ~/.bashrc

Test your alias

Running the command nano doof.txt should create a backup file (the backup will have a tilde (~) appended to it). The backup is only created if you save the file.

Running the command rm doof.txt will result in confirmation request “rm: remove regular empty file 'doof.txt'?

And that’s it! This is how you create aliases in Ubuntu 16 via the commandline