What is Alias in Linux? [Creating, Removing & More]

Alias is a string shortcut that can replace a particular widely used command. This helps to get rid of the repetitive use of a long command, thus increasing efficiency while saving time. This command can be used to replace a command with user-modified instructions while running the command. In this article, you will learn the features and aspects of aliases in Linux.

How to List Currently Defined Aliases in Linux?

By default, there are some built-in aliases in the Linux machine. You will be able to see those by running alias command.

After that, you will be able to see the lists of aliases.

The built-in list of aliases in our machine by default

Linux Alias Synopsis

By using the type command we can see that this is a shell built-in command. We will not be able to find the man page for the alias. We have to use the help command to know the synopsis of the alias.

Synopsis of alias in Linux containing its type, man page and help section.

Create a Alias in Linux

To create a temporary alias in Linux, use alias <name> = "command" syntax. For example, consider the following:

alias today = “date”

In this command line, I used today shortcode to replace the date command.

Creating temporary alias in LinuxSo we have just created an alias for the date command. Now if we type today as the command we will get the same output as the date command. Thus aliasing the date command by just a word today makes it easier.

Note: As this one is a temporary alias, you won’t find it or be able to use this after opening a new terminal session. Temporary alias cannot be used after starting a new session in terminal

How to Create a Permanent Alias in Linux?

To create a permanent alias in Linux, add the alias to .bashrc file.  Here’s how:

  1. Open the terminal type the following command in the command prompt to open .bashrc file:
    nano .bashrc
  2. Scroll down to the alias portion.
  3. Add your desired alias command line as you wrote in the temporary file.Putting alias command in .bashrc file to create permanent alias in Linux
  4. Now, press CTRL+X and type Y, and then press ENTER to save all the changes.

    Saving the nano file to create permanent alias

After starting a new terminal session using bash you will be able to use the alias command permanently.

Starting a new session using bash doesn't remove the alias file. It is used permanently now.

How to Remove Alias in Linux?

To remove the alias, use unalias command followed by the alias name you want to remove. For example:

unalias today

This will remove the alias named “today” from the alias list of the system.

Removing alias using unalias command.

Here you can see, after using the “unalias” we can see that there are no such commands in the list of alias we just saved earlier.

How to Use Alias in Linux?

There are lots of useful features of Alias in Linux. Here I will show you one of the most important tricks that you can follow to better your performance.

To remove any file we use the “rm” command. Sometimes we want to keep some files but accidentally we permanently remove them with the “rm” command. If we use “rm -ri” then it will ask for every file whether to remove it or not.

We can make it easier by using this command aliasing to get to know every time whether we want to delete that file or not.

In the screenshot above, you can see that first I was using rm -ri followed by a directory name to remove the directory. Later, I aliased rm -ri with simply rm shortcode to perform the same task. Now it’s more efficient in terms of security and time.

Reducing rm -ri command to rm command.

Conclusion

After completing this article, you will be able to understand all the features and aspects of aliases in Linux. You will be able to create, remove, and use the alias to get your desired results. I hope it will help you to subjugate your machine perfectly.

People Also Ask

How do I create an alias?

Use the alias command followed by the alias name, an equal sign, and the command you want to alias. For example: alias ll='ls -l'.

Can I remove or modify existing aliases?

Yes, you can remove or modify aliases using the unalias command followed by the alias name, or by editing the shell configuration file directly.

Where are aliases stored in Linux?

Aliases are typically stored in the user’s shell configuration file, such as .bashrc or .bash_profile.

Are aliases persistent across terminal sessions?

Yes, aliases defined in shell configuration files persist across terminal sessions once they are defined.

Can I list all defined aliases?

You can list all defined aliases using the alias command without any arguments.

Can I set alias commands permanently?

Yes, permanent aliases are typically defined in system-wide configuration files, such as /etc/profile or /etc/bash.bashrc.

Are aliases available in all Linux distributions?

Yes, aliases are a standard feature in most Linux distributions and Unix-like operating systems.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Borhan Uddin

Hello, I am Borhan Uddin, a resident of Dhaka, Bangladesh. I have completed my undergraduate degree in Electrical and Electronic Engineering (EEE) from Chittagong University of Engineering and Technology (CUET). I love to spend my leisure by playing and watching various movies & dramas. Currently, I am working as a Linux Content Developer Executive here. I would like to learn more about Linux every day of the week and would be keen to share it with you rapidly. Read Full Bio

Leave a Comment