Minimizing the time for any work is the most wanted thing in our day-to-day life. Alias is used to save time in our Linux command uses. Long commands used in regular uses are made shorter with an alias. In this article, you will learn the features and aspects of alias in Linux.
What is Alias in Linux?
Alias is a string shortcut that can replace a particular widely used command. This allows getting 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.
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 following the steps given below:
Steps to Follow >
❶ At first open the Ubuntu Terminal by pressing CTRL + ALT + T.
❷ Type the following command in the command prompt:
alias
❸ Press the ENTER button.
Output >
After that, you will be able to see the lists of aliases.
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.
Create a Temporary Alias in Linux
To create a temporary alias in Linux you just need to follow a few easy steps. The steps are mentioned below:
Steps to Follow >
❶ Open the terminal first.
❷ Type the following command in the command prompt:
alias
❸ After that, type an arbitrary shortcode that you want to use to replace your targeted command.
❹ Then type an equal sign “=” followed by the command that you want to alias inside double quotes (“”).
For example, consider the following syntax:
alias today = “date”
In this command line, I used today shortcode to replace the date command.
❺ Finally, press the ENTER button.
Output >
So 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.
Create a Permanent Alias in Linux
You can create a permanent alias in Linux by using the .bashrc file. The steps are given below:
Steps to Follow >
❶ Open the terminal first.
❷ Type the following command in the command prompt:
nano .bashrc
It will open the .bashrc configuration file.
❸ Scroll down to the alias portion.
❹ Add your desired alias command line as you wrote in the temporary file.
❺ Now, press CTRL+X and type Y, and then press ENTER to save all the changes to your bash configuration file.
Output >
After starting a new terminal session using bash you will be able to use the alias command permanently.
Here we can also see the new list of aliases in our machine where the permanent alias has been added successfully.
How to Remove Alias in Linux
To remove the alias we just need to follow a few easy steps. The steps are mentioned below:
Steps to Follow >
❶ Open the terminal first.
❷ Type the following command in the command prompt:
unalias
➌ After that, type the desired command name you want to remove.
➍ Finally, press the ENTER button.
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.
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.
Similar Readings