How to use the Linux alias command to create command shortcuts

You can use the Linux alias command to create shortcuts for longer commands, making them easier and quicker to use. The alias command for Linux will be removed from the system when shutting it down if you don’t permanently save it. If you want to restore the original settings you had, you can use the unalias command.

What is the Linux alias command?

There are some Linux commands which you will use on a regular basis. If these commands turn out to be quite long or are complete command chains, you need to enter them in full every time. This can be annoying, wastes time and you might also end up making a mistake. This is where the Linux alias command comes in. You set this up once and it works as a custom shortcut used to represent a command. The big advantage of this is that it only needs to be a few characters long and can complete a whole chain.

The Linux alias command is, however, temporary. It’s automatically disabled when you close the console or log off. You can find out below how to get around this. To manually end the Linux alias command, you can use the command unalias. If you want an overview of all your current alias commands, you can simply execute the following:

$ alias
shell

How does the alias command work?

When you create an alias command, you get a shortcut to a long command or command chain. To do this, you set the command’s alias to a longer command. The system will then carry out the longer command even when you enter the shortcut. As already explained, however, this is only the case for the rest of your session. The system will then stop using it.

What does the alias syntax look like?

This is the general alias command syntax:

$ alias shortcut="The command you want to use with alias"
shell

The “alias” is the command itself, “shortcut” is the shortcut you can specify and the part after the equals sign is the full command you want it to run.

Here is a practical example for the Linux alias command:

$ alias c="clear"
shell

If you want to clear the content on your screen in the terminal, you simply need to enter the shortcut “c”.

What options does the alias command have?

You have two options with the alias command:

  • -p: This option will show you all the aliases in the terminal so that you can use them again.
  • –help: This options gives you general help about using the alias command.

How can I use the aliases permanently?

To use an alias permanently in Linux you have two different options which require you to use a code editor.

With .bash aliases

  1. First, you create a file called ~/.bash_aliases with the editor of your choice.
  2. You can then enter all the commands you wish to use.
  3. You can now close and save the file.
  4. Now open .bashrc and enter the following code:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
shell
  1. This will read the alias file.
source ~/.bash_aliases
shell

With .bashrc

  1. Open the file ~/.bashrc with your editor.
  2. Then look for the section “Alias definitions”.
  3. Here you’re able to create the shortcuts using the alias command.
  4. You can now close and save the file and restart your session.
  5. Reopen the file again. To do so you can use the following command:
source ~/.bashrc
shell

Examples of the Linux alias command

You can generally use the Linux alias command for all different types of command. However, it’s highly recommended for those that have a high error rate:

$ alias ll="ls | less"
shell

By using this command, the ls command is changed to less so that longer outputs can be read on a page.

$ alias up="sudo apt update && sudo apt upgrade"
shell

This combines both commands to update packages.

$ alias des="cd ~/Desktop"
shell

This Linux alias allows you to go directly to your desktop.

We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.