Do you still type the long βsudo apt update && sudo apt upgrade -y
β commands each time you want to update your Debian-based system?
Or are you still navigating into directories (ex: β/var/www/html
β) using the cd command each time you have to modify something there?
Instead of typing these long commands, you can shrink them into a single short command and use that to execute them each time as needed.
Tutorial Details
Description | Shrinking Long or Multiple Commands into a Single Short Command |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | alias, vim, nano |
Internet Required | No |
Shrinking Long or Multiple Commands into a Single Short Command
To shrink any long or multiple commands, you can create an alias for those commands using the alias command.
For example, if you want to use the βupdate
β keyword to update your system instead of typing the long βsudo apt update && sudo apt upgrade -y
β commands.
Then use the alias command to replace the long command with the short command, as shown.
$ alias update="sudo apt update && sudo apt upgrade -y"
Output:
Another example,
If you are a web developer and require jumping into the β/var/www/html
β directory multiple times to modify the file, then right now you are going through the cd command, specifying the full path for the directory.
However, you can shrink this command with a single command, as shown.
$ alias html="cd /var/www/html"
Output:
Note that right now, the changes are being made temporarily in your shell child process environment.
To make them permanent, you have to add this command to your shell configuration file.
Apply the Changes Permanently to your Shell Environment
To apply the changes permanently, you need to add the command (ex: βalias ...
β) in your shell configuration file.
However, there are two types of configuration files that most Linux distributions use to look for aliases.
- Global shell configuration files are located at β
/etc/environment
β and β/etc/profile
β. If you specify the commands in these files, the changes are accessible to all users. - Specific user configuration files are located at β
~/.bashrc
β for Bash, β~/.zshrc
β for ZSH, and β~/. config/fish/config.fish
β for Fish. If you specify the commands in these files, the changes are accessible only to the currently logged-in user.
For example, if you want to apply the changes to a specific user, find their default shell using the following command:
$ echo $SHELL
/bin/bash
#OR
$ echo $0
bash
As you can see, the default is the bash shell, so next edit the bash configuration file β~/.bashrc
β using your choice of text editor, like Vim or Nano.
$ vim ~/.bashrc
#OR
$ nano ~/.bashrc
And add all of your aliases at the end of the file.
#Custom Aliases
alias update="sudo apt update && sudo apt upgrade -y"
alias html="cd /var/www/html"
Output:
Save and close the file, and load the new aliases in your current shell environment using the source command.
$ source ~/.bashrc
To verify the changes, you can use the short commands that you added to your aliases.
In the future, if you want to undo the changes or rename the aliases, you can simply edit the shell configuration file, make the modifications you want, and source the shell configuration file to apply the changes.
Thatβs all for this article.
If you have any suggestions, feel free to tell us in the comment section.
Bash shells allow permanent storage of aliases in the .bash_aliases file. If your distribution supports this the .bashrc file in the user home directory will have these lines in them:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi