How to Create an Alias and Use the Alias Command in Linux

The alias command in Linux lets you create shortcuts for commands, making them easier to remember and use. Here's how to create them!

Are you tired of typing the same commands over and over? Unfortunately, as system administrators, we inevitably repeatedly use the same commands.

A Bash alias is a method of supplementing or overriding Bash commands with new ones. So, you can save time by creating aliases for your most-used commands.

What is an Alias in Linux?

An alias is a custom command created by the user to execute another, usually more complicated command or group of commands.

Therefore, it is a shortcut to reference a command. In other words, an alias is a shortcut command which will have the same functionality as if we are writing the whole command.

For example, you could set the alias tgz as a shortcut for the tar xvzf command.

You’ve found it very helpful to create aliases to make your command line life easier.

List Currently Defined Aliases

You can see a list of defined aliases on your profile by simply executing the alias command.

alias
List Currently Defined Linux Aliases

Above, you can see the default aliases defined for your user. For example, as you can see, executing ll is equivalent to running ls -alF.

You can create anything from simple shortcuts to powerful custom commands using aliases like this.

How to Create an Alias in Linux

Creating an alias in Linux is very easy. The syntax is as follows:

alias alias_name='command_to_run'Code language: JavaScript (javascript)
  1. Start with the alias command.
  2. Then type the name of the alias you want to create
  3. Add an = sign, with no spaces on either side of the =
  4. Then type the command (or commands) you want your alias to execute when it is run. This can be a simple command or a powerful combination of commands.

You can either enter them at the command line as you’re working or, more likely, put them in one of your startup files, like your .bashrc file, so they will be available every time you log in.

An alias in Linux can be directly set in the shell as follows:

alias ll='ls -alF'Code language: JavaScript (javascript)

When the ll command is run, the command will use the alias and the -alF option.

Note that setting an alias in Linux in this way only works for the life of a shell session. When the shell is closed, the alias will be lost.

A shell configuration file should be used to make an alias persist across shell sessions and reboots. For Bash, this is the .bashrc file.

The .bashrc file is located in your home directory. So open the file in your text editor:

vim ~/.bashrc

Now find a place in the file where you want to keep the aliases. For example, you can add them at the end of the file:

Setting aliases in the .bashrc file

Save the file. The new aliases automatically load in the next terminal session.

However, if you want to use them in the current session, reload the .bashrc file using the following command:

source ~/.bashrc

How Do I Remove an Alias in Linux?

You can use the unalias command with the following syntax to remove an alias in Linux:

unalias [alias_name]Code language: CSS (css)

For example, let’s assume we have an alias named count, which counts the number of files in the current directory.

To remove it, we need to execute:

unalias count
Delete an alias in Linux

Of course, if the alias is saved in the .bashrc file, we need to delete the line that defines it.

If we want to delete all existing aliases at once, we can use the command below:

unalias -a

Conclusion

While this Linux alias is a simple quality of life change, you can apply this same concept to long and complicated commands that you might have to use routinely during your daily work.

So, now you can think about the commands you use the most and create shortcuts for them in your shell.

For more about the alias command in Linux, consult its manual page.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%