Tai Hoang

Define Alias for Long Commands in Linux

In Linux, you can create Aliases, think of it as shortcuts, for long commands, which is very useful and save time when you have to work with really long commands like deeply nested path or scripts that has multiple flags and options.

When creating an Alias, you can make it temporary or permanent. A temporary Alias only exist in your current session and will disappear after closing the shell or logging out. A permanent Alias is written in your Shell config to make it always active even after you shut down the machine.

Create a Temporary Alias

In the terminal, enter this command:

alias update='sudo apt update'Code language: Shell Session (shell)
  • alias: The keyword to define an Alias.
  • update: The alias name, you can choose any name that you like.
  • sudo apt update: The actual command to be referred to.

To verify if you have successfully created it, enter the command alias in your terminal to show a list of all current Aliases.

Now you can just enter update in your terminal and it will work as the same as sudo apt update.

Create a Permanent Alias

To make the Alias permanent, you have to add it to your shell’s config file, usually ~/.bashrc (on Linux) or ~/.zshrc (on macOS).

Open your shell config file:

nano ~/.bashrcCode language: Shell Session (shell)

Add your Alias at the end of the file:

alias update='sudo apt update'

# You can add multiple Aliases too
alias ll='ls -la'
alias shutdown='sudo shutdown now'Code language: Shell Session (shell)

Save the file and exit.

Enter this command to apply the changes:

source ~/.bashrc

# OR
source ~/.zshrcCode language: Shell Session (shell)

Now you Aliases will always be there when you need them.

Reply via email