WriteBash - Use alias command to create custom commands. This article will show you how to create and use it in Linux.
In the article about the types of commands available in Linux, I mentioned the
alias command. Now I will say more about it.
The purpose of the alias command in Linux
There is a fact that takes place in Linux system administration, there are very long commands that we have to execute. And even, we may have to execute it many times every day.
You will be tired of repeating those long commands. And the alias command solves that problem.
So what does it do, it simply creates a custom command that represents your long command line. And instead of having to type the long command, you have a custom command.
Use alias in Linux
In the article I mentioned above, a command I have to repeat many times when working with InfluxDB is the login to its command window.
# influx -ssl -unsafeSsl -host 192.168.10.10
And now, I want a more concise command to type instead of the long command above. For example, I want the custom command to be
Recommended Reading: Use “type” command to display a command’s type
Create temporary alias
To create temporary alias, we use the command structure below.
# alias custom_command='string'
custom_command: the name of the custom command you want to use.
string: Long string of commands that you need to use alias instead. In this example, the main string is
influx -ssl -unsafeSsl -host 192.168.10.10
Now type the following command to create alias.
# alias login_influx='influx -ssl -unsafeSsl -host 192.168.10.10'
Now you can type
login_influx to see if it is available.
Why is it temporary when you create alias as above? Because this alias command is only loading in the current terminal session. If you exit the terminal session that is working, this alias will be deleted.
Create alias permanently
To fix the temporary alias, we will create alias permanently. Whenever you login to the terminal, alias will be there for you to use.
Now open the
~/.bashrc file and add the above alias command to the end of the file.
Or you can run the following command to quickly add the file to the end.
# echo "alias login_influx='influx -ssl -unsafeSsl -host 192.168.10.10'" >> ~/.bashrc
This file works to run alias for your session.
And then, you run this command to reload the
~/.bashrc file immediately.
# source ~/.bashrc
Now try typing the
login_influx command on the terminal.
The commands work with alias
List all available alias commands.
~ $ alias
Delete an alias of a certain command. With example above.
~ $ unalias login_influx
Delete all alias commands.
~ $ unalias -a
The alias command can be said to be one of the most useful commands in Linux. I myself use it very often, and believe me, you will be the same.
Hopefully with the above article, you can save time during your work. Help it less boring.If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.
If you appreciate what we share in this blog, you can support us by:
- Stay connected to: Facebook | Twitter | Google Plus | YouTube
- Subscribe email to recieve new posts from us: Sign up now.
- Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
- Become a Supporter - Make a contribution via PayPal.
- Support us by purchasing Ribbon Lite Child theme being using on this website.
We are thankful for your support.