In the next article we are going to take a look at the alias tool. Gnu / Linux users often need use the same command over and over again. Typing or copying the same command so many times can reduce productivity and can distract you from what you're really doing.
We can save ourselves some time creating aliases for our most used commands. These are kind of like custom shortcuts. Used to represent a command (or set of commands) executed with or without custom options.
Some do not recommend the use of tools like this, since despite its great utility, its use can be counterproductive. Especially for users who are starting in the Gnu / Linux world and its terminal. Since while it can be very useful and friendly using custom commands, it can also make us forget the real commands.
List aliases on your Ubuntu
This tool is already installed by default in our Ubuntu. To use it we will only have to edit the .bashrc file that is in the Personal Folder, in a hidden way.
First of all, we will be able to see a list defined in our profile just running this command in terminal (Ctrl + Alt + T):
alias
Here you can see the User defined default aliases in Ubuntu 18.04. As indicated in the screenshot, executing in the terminal (Ctrl + Alt + T) the command «la»Would be the equivalent of running:
ls -A
We will be able to create one of these links with a single character. It will be the equivalent of a command of our choice.
How to create aliases
Creating them is a relatively quick and easy process. Anyone can create some of these two types: temporary and permanent.
Create temporary aliases
What we have to do is write the word alias in the terminal. Then we will have to use the name that we want to use to execute a command. This will be followed by the '=' sign and the call to the command we want to use.
The syntax to follow is the following:
alias nombreAlias="tu comando personalizado aquí"
This would be a real example:
alias htdocs=”cd /opt/lampp/htdocs”
Once defined we will be able to use the 'htdocs' shortcut to go to the htdocs directory. The problem with this shortcut is that 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 want to save them between sessions, you will need a permanent one.
Create permanent aliases
To keep the aliases between sessions, you will have to save them in the profile file for your user's shell configuration. These could be:
- bashes → ~/.bashrc
- Z SH → ~ / .Zshrc
- Fish → ~ / .config / fish / config.fish
The syntax to use in this case is the same as when we create a temporary one. The only difference comes from the fact that this time we will save it to a file. So for example in bash, you can open the .bashrc file with your favorite editor:
vim ~/.bashrc
Inside the file, find a place in the file to save the aliases. A good place to add them is usually at the end of the file. For organizational purposes, you can leave a comment before:
#Mis alias personalizados alias imagenes=”cd /home/sapoclay/Imágenes/” alias actualizarsistema=”sudo apt update && sudo apt upgrade” alias pingxbmc="ping 192.168.1.100"
When finished save the file. This file will be uploaded automatically in your next session. If you want to use what you just wrote in the current session, run the following command:
source ~/.bashrc
We will also be able to have our aliases in a separate document. To define a permanent alias you just have to follow the instructions that the bashrc file shows us. We will be able to have a separate file called bash_aliases to store them.
Everyone we create in that file will work the next time we open a new terminal. To apply the changes immediately we can use the following command:
source ~/.bash_aliases
Remove aliases
For remove an alias added via command line, you can use the unalias command.
unalias nombre_del_alias
In case of wanting remove all alias definitions, we can execute the following command:
unalias -a [elimina todos los alias]
Keep in mind that the unalias command also applies only to the current session. To permanently remove one, we must remove the appropriate entry in the ~ / .bash_aliases file.
Another thing to keep in mind is that if we have a permanent alias and we add a temporary one during the session with the same name, the temporary will have higher privileges during the current session.
This was a small guide example of how to create our own aliases to run frequently used commands. For know more about this tool, you can consult the article written in the Wikipedia.
Terrific, I loved it !!! served me perfect.