alias command in Linux with examples

Alias command  is an extremely useful tool. You’ve probably noticed that, in the Linux command line, you have to do a lot of typing. And sometimes, this can pose some problems as it is very easy to make mistakes while typing. A very common typo that many users make in the Linux command line terminal is sl instead of ls. This can be quite annoying as you will have to retype the entire command. Linux is designed to be fast, convenient, and efficient, so when things don’t exactly play out that way, it can result in a decent amount of frustration.

So, to prevent you from dealing with any of that, you can use the alias command in Linux. The most common application of this command is to replace typos with the desired command or to add more options to a commands. So, you can just focus on typing as quickly as possible without worrying as much about minor typos or adding the same options to commands every time you execute them.

In this tutorial, you’ll learn how to use the alias command in Linux through examples. Follow along below to learn about the various options that you can use with this command.

In this tutorial you will learn:

  • How to use the alias command on Linux

"<yoastmark

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software alias
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options

The alias command replaces specified words with another word that you can use to add default options to a command or correct typos on your shell command line. Unlike most basic Linux commands, the alias command does not come with a lot of options. But there are a couple that is very much worth committing to memory. We’ll cover all of them below in the examples section of this article.

alias command in Linux Basic Examples

  1. Running the alias command by itself, without any additional options, will just replace the specified word in the command line with another word. As mentioned earlier, we’ll use the ls command as an example.
    $ alias sl=ls
    

    "Running

    As you can see in the screenshot above, the output will make it so that running the sl typo will list the contents of our current working directory, which is what the ls command does.

  2. One of the very few command line options that are available for the alias command is the -p option. If we pass this command line option to the alias command, it will output a list of every alias that we have defined in our Linux terminal.
    $ alias -p 
    

    "Using

    As you can see in the screenshot above, the alias command prints the list containing our aliases, including but not limited to the sl alias that we created in the previous example. The rest of the aliases listed in the screenshot were configured by default on our system.

  3. In the examples above, we showed how to create aliases using the alias command in your Linux terminal to correct typos automatically. But these aliases that we have made, as you may have noticed, do not like to stick around after you end your session in your terminal.This is because the alias command does not create permanent aliases but rather temporary ones that won’t survive reboots. But there is a neat little trick that we can utilize to create aliases that are more resilient. To do this will require that we add our custom aliases to the ~/.bashrc file.
    $ nano ~/.bashrc
    

    Then, add your alias to the bottom of the file to create a persistent alias. Exit and save changes when done.

    alias sl=ls
    

    Type the following command or reboot your system for the new changes to take effect. To remove the alias later, you will have to delete it from the file.

    $ source ~/.bashrc
    

    Defining an alias and typing it into ~/.bashrc to make it persistent
    Defining an alias and typing it into ~/.bashrc to make it persistent
  4. We can also remove temporary aliases with the unalias command.
    $ unalias sl
    

    "Using

NOTE
You can always use the man command to read more about the alias command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Closing Thoughts

In this tutorial, we learned all about the alias command which is beneficial to master for general users and system administrators that frequently use the Linux command line terminal.

Remember that using the alias command for correcting typos was an example of only one of the possible usages of this command. You can use the alias command to replace any word with another word of your choosing, or add additional options to any default command.