How to Shrink Long or Multiple Commands into a Single Short Command

Linux TLDR
Last Updated:
Reading time: 2 minutes

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

DescriptionShrinking Long or Multiple Commands into a Single Short Command
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesalias, vim, nano
Internet RequiredNo

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:

Shrinking a long command into a small command

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:

Shrinking long directory path into short

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:

Custom aliases in the bash configuration file

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.

Verifying the newly added aliases in the shell configuration file

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.

1 thought on β€œHow to Shrink Long or Multiple Commands into a Single Short Command”

  1. 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

    Reply