Back in the old days of computers, I remember running DOS 3.3. I had a folder named 'batch' that included all the 'shortcuts' to perform commands by a batch file. Of course, the 'batch' folder was in my environment variable 'PATH'. So, no matter what directory I was in, I could run one of the batch file that acted as a shortcut.
In this same manner, we can use an 'Alias' to shorten a set of command instructions to a single command word.
Using aliases allows you to shorten long command strings into a single word and save time on your system.
Basic Use
You may need to see what aliases you already have on your system. The best way is to list your existing aliases:
The command 'alias' with no parameters just provides a list of existing aliases. Figure 1 shows an example of the output.
FIGURE 1
You can also use the parameter '-p' to print the list of aliases, which gives the same result.
An alias is 'alias short name = command string'. Let's make an example. For instance, if we want the 'ls' command to show all the files, we can create an alias for 'ls' to always be 'ls -a'. We can use the command:
Now, when we type 'ls', we get a listing of all files and folders, even the hidden ones.
If we reboot the system or even close the terminal and re-open it, the alias is gone.
Making an Alias Permanent
If we want an alias to persist after rebooting or closing and opening the terminal, we need to add the alias to the environment.
Within the '~/.bashrc', there is a section:
We can add the alias commands to the '.bashrc' file or create and add the commands to '~/.bash_aliases'.
No matter the file you use, place the complete command in the file. Such as 'alias ls-"ls -a"'. You can close and re-open the terminal, then enter 'alias' to make sure your additions are active.
The example I gave is short, but we can lengthen the alias to something like:
Not only does the command 'up' perform an update of the repository lists, but performs an upgrade with 'yes' as the default answer to any queries by the system. It also performs an 'autoremove' of unneeded packages, again answering 'yes' to any queries. Finally, the aliased command will perform distribution upgrades as well. You can, of course, change the order of the commands as you need.
Keep in mind that aliases can also be case-sensitive. An alias of 'lsa' differs from 'LSA' and even 'LSa'.
Issues
Be sure that you do not alias an existing command that you do not want to lose. For example, we previously changed the 'ls' command to show even hidden files, but we may not want hidden files displayed every time we list files.
To prevent this from happening, you can type a command you want to create and see if it exists. If it doesn't exist, then an error will occur. You can also use the command 'type' followed by the command you want to alias, such as 'type up'. The response will let you know if the command 'up' is being used at all.
Removing an Alias
If you did not add the alias to the '~/bashrc' file, then you can remove the alias from the current environment. If the alias is permanent by being in the '~/.bashrc' file, you can remove it from the current terminal environment, but when you restart the terminal, it’s read from the file when the terminal starts again.
To remove an alias from the current environment, use the command:
So, we can use the command 'unalias ls' to remove the 'ls' command we set up. Or even 'unalias up'. You can then type 'up' and it shouldn't work. Close the terminal and restart it, and the 'up' command should work again.
If you include the command in the '~/.bashrc' file or another file, like '~/.bash_aliases', you need to remove the line from the file. The terminal needs to be closed and restarted so the changes will take effect.
If you want to remove all aliases from the current environment, then use the command 'unalias -a'.
Other Possibilities
An alias can call a script. Here, you can have powerful scripts being called that are included in an alias.
This can be especially useful if the script is called within a group of commands that you usually run. Now, an alias can run all the commands in one, including the script.
Useful Examples
From the old DOS days, you used to do a 'cd..' to go up a folder in the tree, but in Linux, there must be a space. So you can use the alias:
Another possibility is to use an alias for 'edit' to your preferred editor:
Just make sure after changing the 'bashrc' files, exit the terminal and restart it.
Checking Individual Aliases
Let's say we set an alias for the command 'edit' and used it with our favorite editor.
We can check the alias with the command 'alias' followed by the command, such as:
The result should be the command used to set up the alias for 'edit':
If an alias isn't working the way we thought, we can see the command to verify there are no typos in setting up the alias.
Conclusion
Alias is a great way to shorten multiple commands into one. You can set the aliases to include specific parameters so you do not have to type them every time.
If needed, keep the alias file handy to put onto new systems so your aliases are always available.
In this same manner, we can use an 'Alias' to shorten a set of command instructions to a single command word.
Using aliases allows you to shorten long command strings into a single word and save time on your system.
Basic Use
You may need to see what aliases you already have on your system. The best way is to list your existing aliases:
Code:
alias
The command 'alias' with no parameters just provides a list of existing aliases. Figure 1 shows an example of the output.
FIGURE 1
You can also use the parameter '-p' to print the list of aliases, which gives the same result.
An alias is 'alias short name = command string'. Let's make an example. For instance, if we want the 'ls' command to show all the files, we can create an alias for 'ls' to always be 'ls -a'. We can use the command:
Code:
alias ls="ls -a"
Now, when we type 'ls', we get a listing of all files and folders, even the hidden ones.
If we reboot the system or even close the terminal and re-open it, the alias is gone.
Making an Alias Permanent
If we want an alias to persist after rebooting or closing and opening the terminal, we need to add the alias to the environment.
Within the '~/.bashrc', there is a section:
Code:
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
We can add the alias commands to the '.bashrc' file or create and add the commands to '~/.bash_aliases'.
No matter the file you use, place the complete command in the file. Such as 'alias ls-"ls -a"'. You can close and re-open the terminal, then enter 'alias' to make sure your additions are active.
The example I gave is short, but we can lengthen the alias to something like:
Code:
alias up='sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y && sudo apt dist-upgrade -y'
Not only does the command 'up' perform an update of the repository lists, but performs an upgrade with 'yes' as the default answer to any queries by the system. It also performs an 'autoremove' of unneeded packages, again answering 'yes' to any queries. Finally, the aliased command will perform distribution upgrades as well. You can, of course, change the order of the commands as you need.
Keep in mind that aliases can also be case-sensitive. An alias of 'lsa' differs from 'LSA' and even 'LSa'.
Issues
Be sure that you do not alias an existing command that you do not want to lose. For example, we previously changed the 'ls' command to show even hidden files, but we may not want hidden files displayed every time we list files.
To prevent this from happening, you can type a command you want to create and see if it exists. If it doesn't exist, then an error will occur. You can also use the command 'type' followed by the command you want to alias, such as 'type up'. The response will let you know if the command 'up' is being used at all.
Removing an Alias
If you did not add the alias to the '~/bashrc' file, then you can remove the alias from the current environment. If the alias is permanent by being in the '~/.bashrc' file, you can remove it from the current terminal environment, but when you restart the terminal, it’s read from the file when the terminal starts again.
To remove an alias from the current environment, use the command:
Code:
unalias <alias>
So, we can use the command 'unalias ls' to remove the 'ls' command we set up. Or even 'unalias up'. You can then type 'up' and it shouldn't work. Close the terminal and restart it, and the 'up' command should work again.
If you include the command in the '~/.bashrc' file or another file, like '~/.bash_aliases', you need to remove the line from the file. The terminal needs to be closed and restarted so the changes will take effect.
If you want to remove all aliases from the current environment, then use the command 'unalias -a'.
Other Possibilities
An alias can call a script. Here, you can have powerful scripts being called that are included in an alias.
This can be especially useful if the script is called within a group of commands that you usually run. Now, an alias can run all the commands in one, including the script.
Useful Examples
From the old DOS days, you used to do a 'cd..' to go up a folder in the tree, but in Linux, there must be a space. So you can use the alias:
Code:
alias cd..="cd .."
Another possibility is to use an alias for 'edit' to your preferred editor:
Code:
alias edit=nano
Just make sure after changing the 'bashrc' files, exit the terminal and restart it.
Checking Individual Aliases
Let's say we set an alias for the command 'edit' and used it with our favorite editor.
We can check the alias with the command 'alias' followed by the command, such as:
Code:
alias edit
The result should be the command used to set up the alias for 'edit':
Code:
alias edit='nano'
If an alias isn't working the way we thought, we can see the command to verify there are no typos in setting up the alias.
Conclusion
Alias is a great way to shorten multiple commands into one. You can set the aliases to include specific parameters so you do not have to type them every time.
If needed, keep the alias file handy to put onto new systems so your aliases are always available.
Last edited: