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 it each time as it requires.
|Description||Shrinking Long or Multiple Commands into a Single Short Command|
|Root or Sudo Privileges||No|
|Host System and Architecture||Ubuntu 22.10 (x64)|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
|Prerequisites||alias, vim, nano|
|Discussed Tools in this Article|
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.
Than use the alias command to replace the long command with the short command, as shown.
$ alias update="sudo apt update && sudo apt upgrade -y"
If you are a web developer and require jumping into the “/var/www/html” directory multiple times to modify the file, than 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"
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.
Also Read: How to Set and List Environment Variables in Linux
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.
Also Read: Difference Between /etc/profile, ~/.bash_profile, ~/.profile, ~/.bashrc, etc
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"
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.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.