How to Set Up Command Aliases in Linux/Ubuntu/Debian

How to Set Up Command Aliases in Linux/Ubuntu/Debian

If you don’t know how to set up aliases in Linux, you should be very happy you have reached this page! Aliases are one of the most time-saving devices known to man. An alias is a way to make a complicated command or set of commands simple. This is best demonstrated by an example.

In web development or computer programming, there are a lot of times you need to recompile some source file. Below we are recompiling a less file and looking at the tail of the result.

Note: The example below can be generalized to any set of commands.

Steps to Setting Up Aliases in the bash-shell

Wouldn’t it be easier to just type something like the following?

Luckily for us, this is simple to do in the bash-shell.

1. Open your .bashrc.

Your .bashrc file is located in your user directory. Open it in your favorite text editor.

2. Go to the end of the file.

In vim, you can accomplish this just by hitting “G” (please note that it is capital).

3. Add the alias.

A simple way to chain commands in Linux is to use the && operator. This operator will run a set of commands and only continue to the next command if the previous one was successful.
For our example, we might have an alias that looks like this:

This looks complicated but it really isn’t.  Here’s the basic format:

One gotcha is that there cannot be a space between the “aliasname” and the EQUAL sign. Also, there can’t be a space between the EQUAL sign and the opening quote for the command.

4. Write and close the file.

In vim, hit ESCAPE to get to normal mode and run the following command to write and quit:

5. Install the .bashrc.

The new .bashrc would be installed the next time you log out and log back in, but if you are impatient like me and just want it installed now, you can just source the file.

Well, that’s it. You now can alias until your heart’s content. Remember, a few seconds saved here and there can dramatically increase your efficiency!

Ryan Frankel

Questions or Comments? Ask Ryan!

Ask a question and Ryan will respond to you. We strive to provide the best advice on the net and we are here to help you in any way we can.

  • Akshay

    Good one! Thanks boss.. 🙂

    • frankel0

      No problem! Glad it helped.

  • Pierangelo Castillo Mora

    nice ^_^ what a time saver indeed!

    • frankel0

      Be careful, aliases can be addicting.

  • Trong Nguyen

    I used “. ~/.bashrc” to restart the bash and I can use alias immediately after that. It’s just my two cents. Nice tip indeed, thank you Ryan.

    • frankel0

      In some sort of alias inception, . is an alias for source so . ~/.bashrc is equivalent to source ~/.bashrc. I used the latter in the article so that people would see explicitly what was happening.

  • dsaffasdsd

    thank you!, you have a typo in capitol just a heads up.

    • frankel0

      dsaffasdsd, what typo?? =)
      (but actually, thanks for noticing that. It has been fixed.)

  • i3igmind

    Thank you, very helpful. Though I have a question, in your example, is that a typo or you intencionaly didn’t include the closing ‘ ?

    • frankel0


      Good catch! That definitely wasn’t “intencional”. I will get that fixed ASAP.

  • T_Bagwell

    Thank you so much Ryan for the post, very insightful. I have lmp_fedora executable file located in /src. I use this command: mpirun -np 4 lmp_fedora > ~/.bashrc and my motive is to be able to execute lmp_fedora from an directory. Now i am stuck on what should my job submit command be for that to happen…your help will be much appreciated.

  • Thank you for such a simple description.

    • frankel0

      Our pleasure.

  • Dan Shires

    Is there a way to set up an alias to also prompt you for a variable – like I want to grep a particular name out of a log file. ‘tac logfile | grep – i $nameIwant’

    • frankel0

      You could set up a bash function in your .bashrc to accomplish that:

      function dangrep() { tac $1 | grep -i $2 }

      or something similar. Though I’m not sure why you need to tac and can’t just grep the file.

  • mol.ha

    I think it should be “$ sudo vim ~/.bashrc” if one is not su. Otherwise an error of
    ‘E212: Can’t open file for writing’ will occur when trying to write, even with “:wq!”.

    • frankel0

      You don’t want to do that…

      It definitely should be just:

      $ vim ~/.bashrc

      to change your user’s .bashrc. If that doesn’t work then your users permissions are probably incorrect.

  • Jason Robert O’Kennedy

    echo “alias aliasname=’command'” >> ~/.bash_aliases && source ~/.bash_aliases