Introduction
The alias command in Linux is a powerful tool that allows you to create shortcuts for commonly used commands. This can save you time and effort when typing commands in the terminal. With an alias, you can create a single command that will execute multiple commands or a command with different options. This tutorial will explain how to create and use aliases in Linux.
How to Create and Use Alias Command in Linux
1. Open the terminal window.
2. Type the following command to open the bash profile:
$ nano ~/.bash_profile
3. Add the following line to create an alias:
alias
4. Save the file and exit.
5. To use the alias, type the alias name in the terminal window.
For example, if you created an alias called “update” for the command “sudo apt-get update”, you can type “update” in the terminal window to execute the command.
Linux users often need to use one command over and over again. Typing or copying the same command over and over again reduces your productivity and distracts you from what you are supposed to be doing.
You can save yourself some time by creating aliases for your most commonly used commands. Aliases are like custom shortcuts that represent a command (or set of commands) that can be executed with or without custom options. Chances are you are already using aliases on your Linux system without even knowing it.
List Currently Defined Aliases in Linux
You can see a list of defined aliases on your profile by simply executing the alias command.
$ alias
Here you can see the default aliases defined for your user in the Ubuntu system.
As you can see, executing the ll command is equivalent to running ls -alF
command.
$ ll $ ls -alF
You can create an alias with a single character that will be equivalent to a command of your choice.
How to Create Aliases in Linux
Creating aliases is a relatively easy and quick process. You can create two types of aliases – temporary and permanent. We will review both types.
Creating Temporary Aliases in Linux
What you need to do is type the word alias then use the name you wish to use to execute a command followed by "="
sign and quote the command you wish to alias.
The syntax is as follows:
$ alias shortName="your custom command here"
Here is an actual example:
$ alias wr=”cd /var/www/html”
You can then use "wr"
shortcut to go to the webroot directory. The problem with that alias is that it will only be available for your current terminal session.
If you open a new terminal session, the alias will no longer be available. If you wish to save your aliases across sessions you will need a permanent alias.
Creating Permanent Aliases in Linux
To keep aliases between sessions, you can save them in your user’s shell configuration profile file. This can be:
- Bash – ~/.bashrc
- ZSH – ~/.zshrc
- Fish – ~/.config/fish/config.fish
The syntax you should use is practically the same as creating a temporary alias. The only difference comes from the fact that you will be saving it in a file this time. So for example, in bash, you can open a .bashrc file with your favorite editor like this:
$ vim ~/.bashrc
Find a place in the file, where you want to keep the aliases. For example, you can add them at the end of the file. For organization purposes, you can leave a comment before your aliases something like this:
#My custom aliases alias home=”ssh -i ~/.ssh/mykep.pem [email protected]” alias ll="ls -alF"
Save the file. The file will be automatically loaded in your next session. If you want to use the newly defined alias in the current session, issue the following command:
$ source ~/.bashrc
To remove an alias added via the command line can be unaliased using the unalias command.
$ unalias alias_name $ unalias -a [remove all alias]
Conclusion
This was a short example of how to create your own alias and execute frequently used commands without having to type each command again and again. Now you can think about the commands you use the most and create shortcuts for them in your shell.
How to Create and Use Alias Command in Linux
Aliases are a great way to save time and effort when using the Linux command line. An alias is a command that is used to substitute a longer command with a shorter one. This can be useful when you need to type a long command multiple times, or when you want to create a shortcut for a command. In this article, we will show you how to create and use aliases in Linux.
Creating an Alias
To create an alias, you need to use the alias
command. This command takes two arguments: the name of the alias and the command that it should execute. For example, if you wanted to create an alias for the ls
command, you could use the following command:
alias ll='ls -l'
This command creates an alias called ll
that will execute the ls -l
command when it is used. You can also use the alias
command to create aliases for multiple commands. For example, if you wanted to create an alias for the cd
command, you could use the following command:
alias cd..='cd ..'
This command creates an alias called cd..
that will execute the cd ..
command when it is used. You can also use the alias
command to create aliases for multiple commands at once. For example, if you wanted to create aliases for the ls
and cd
commands, you could use the following command:
alias ll='ls -l' cd..='cd ..'
Using an Alias
Once you have created an alias, you can use it just like any other command. For example, if you have created an alias called ll
for the ls -l
command, you can use it like this:
ll
This will execute the ls -l
command. You can also use aliases with arguments. For example, if you have created an alias called cd..
for the cd ..
command, you can use it like this:
cd..
This will execute the cd ..
command. You can also use aliases with multiple arguments. For example, if you have created an alias called ll
for the ls -l
command, you can use it like this:
ll /path/to/directory
This will execute the ls -l /path/to/directory
command.
Deleting an Alias
To delete an alias, you need to use the unalias
command. This command takes one argument: the name of the alias that you want to delete. For example, if you wanted to delete an alias called ll
, you could use the following command:
unalias ll
This command will delete the ll
alias. You can also use the unalias
command to delete multiple aliases at once. For example, if you wanted to delete aliases called ll
and cd..
, you could use the following command:
unalias ll cd..
Conclusion
In this article, we have shown you how to create and use aliases in Linux. Aliases are a great way to save time and effort when using the Linux command line. We hope this article has been helpful and that you now have a better understanding of how to create and use aliases in Linux.