DEV Community

Cover image for Making Linux Commands Shorter With Aliases
dev_neil_a
dev_neil_a

Posted on • Updated on

Making Linux Commands Shorter With Aliases

YouTube Video

If you would prefer to watch the content of this article, there is a video version available on YouTube below:

Introduction

This article will be covering:

  • What aliases are in Linux (and macOS).
  • How they are created.
  • How to use them.
  • Determine if there is a alias configured.
  • How to remove one.

What Are Aliases?

In Linux and macOS, aliases are shortcuts for commands that are installed on the system. One of the main purposes for using them is to take a command that an user runs often, has a lot of extra switches / arguments that need to be passed and reduce it down to something simpler and shorter.

Aliases are configured on a per user basis so they are applicable to that user only.

Another common use case for aliases is let's take an user is new to Linux after using Windows for a while. The user is more familiar with using the dir command to get a list of files and folders from the Windows command / PowerShell prompt.

As there is no (built-in) dir command in Linux or macOS, an user would use the ls -lh command to get a similar output to dir (or ls -lha to get the equivalent of dir /a).

With an alias, the user can map the ls -lh command to dir so that they can use what they are familiar with whilst they get use to using the Linux / macOS terminal.

Creating And Using Aliases

To create create an alias, the alias command is used in the following format:

alias <name-of-alias>="<the command the alias should run>"
Enter fullscreen mode Exit fullscreen mode

Now, using the previous dir example, let's create an alias for that which will run ls -lh:

alias dir="ls -lh"
Enter fullscreen mode Exit fullscreen mode

The below image shows what happens when running dir before the alias is created and after:

01-simple-alias

In addition to this, the dir alias can also have additional switches specified after it that are part of the source command. For example, let's run the dir command but this time add on -a (similar function to /a on the Windows dir command) to view all the hidden files and folder in the directory as well as unhidden files and folders:

02-alias-switch

Lastly, an alias can be used to override an existing command. For example, an user creates the following alias:

alias top="htop"
Enter fullscreen mode Exit fullscreen mode

In the above example, when the user runs top (built-in system monitoring tool), it will open htop (user installed system monitoring tool) instead of the top command that most Linux distros ship with.

Making An Alias Persistent

Now, there is one issue with this: The dir alias that was created is only available for the terminal session it was created in. What that means is that if the terminal (or terminal tab) is closed, the alias is lost.

However, an alias can be set to be persistent by adding it to the users shell rc file. As to which rc file will depend upon which shell environment the user is running. The main two shells used are bash (default for most Linux distros) and zsh (default for macOS and some Linux distros). The location of the rc file for each is:

  • bash: ~/.bashrc
  • zsh: ~/.zshrc

To make the dir alias persistent, run the command that is applicable to the shell that is being used:

For bash:

echo 'alias dir="ls -lh"' >> ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

For zsh:

echo 'alias dir="ls -lh"' >> ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

Once that has been done, open up a new terminal session and the alias will work in there and any other sessions that are run.

How To Check For An Alias

To check if the command an user runs is an alias, the where command can be used to see what the path of the command or alias is. For example, an alias called top is configured to use htop but the user would like to run the actual top command this time but doesn't know where it is installed. Using where will show the user the location of the actual top command. For example:

03-where-alias

From the above, there is an alias called top that runs htop. As this is an alias, it is at the top of the search result. With aliases, they have a higher precedence versus commands that are system-wide.

The standard top command can be run in the shell by using the full path.

Removing An Alias

To remove an alias, it is very simple:

  1. Using nano (or another text editor, such as vim), open the rc file that the matches the shell the user is running. For example nano ~/.zshrc.
  2. Locate the alias that needs to be removed and either delete that line or put a # at the beginning of the line to comment it out.
  3. Save the file (CTRL+X followed by y for yes in nano, :wq in vim).
  4. Close the terminal and open a new one. The alias will now no longer work.

Conclusion

To conclude, aliases are a very easy way to help speed up how an user can interact with commands in the terminal and speed up complex & repetitive commands to even a single letter or word.

Thank you for reading and have a nice day!

Top comments (0)