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.
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 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.
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.
How to Create a Permanent Alias in Linux?
To create a permanent alias in Linux, add the alias to .bashrc file. Here’s how:
- Open the terminal type the following command in the command prompt to open .bashrc file:
nano .bashrc
- 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.
After starting a new terminal session using bash you will be able to use the alias command permanently.
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.
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.
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
- What is Directory in Linux [A Complete Overview]
- An Overview of Relative Path in Linux [The Complete Guide]
- An Overview of Absolute Path in Linux [The Complete Guide]
- How to Archive in Linux Using TAR [Archiving, Extraction, Compression]
FUNDAMENTALS A Complete Guide for Beginners