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

      i3igmind,

      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

  • Harikrishna Panigrahi

    You guys told how to create alias..I want to know how to use alias!!

    • frankel0

      When you are at the command prompt, you can just type the alias name to use it.
      – – –
      $ aliasname

      or

      $ tailmyless
      – – –
      would work for the examples shown.

  • Joseph An

    Thank you that was much easier than I thought it would’ve been! I made an alias for rails and android emulator!

  • Dinesh Kandpal

    How can we set a Desktop icon(Iconic Shortcut) for this alias in Ubuntu?

  • Deepa

    Do we need to source bashrc every time we launch we connect to linux machine?

  • adhiana mastur

    Thanks very helpfull

  • Very useful. Was searching for ages and finding all sorts of stuff about mount –bind and binding to function keys on Stack Overflow before this one came up. Always useful when people take time to record the basics in a well written and easy to digest format.

  • for me it worked if I just typed straight into the terminal
    alias aliasname=’commands’
    I didn’t need to navigate and open the .bashrc file first

    • Connor Davis

      this will only work for your current session, and will be cleared when you start a new session (or even open a new terminal). These must be set in the ~/.bashrc file and sourced as the OP states in order for them to continue to work.

  • Rakesh Singh

    Great Info Ryan …you save my lot of time.

  • Sumi Jain

    I added some commands in my ~/.cshrc.user file and sourced it too, still teh commands don’t work and i get this “sdir: Command not found.” ( i defined alias sdir=’cd some/directory/path)

  • Sjors van Heuveln

    ty

  • Anna Forsyth

    Hi, Thanks for this! I just can’t figure out where you ‘escape’ in the instruction “In vim, hit ESCAPE to get to normal mode. I don’t have any button or heading in any of the menu functions that says escape and when I press the ‘X’ in the top right it closes the underlying terminal down?

    • Hopefully you’ve figured it out already, but you just need to hit the Esc button on your keyboard. 🙂

      • Anna Forsyth

        Ha yes sorry! The button on my keyboard was sticky and I didn’t realise; sorry for not resolving it earlier!! Thanks 🙂