logo

How to Set Up Shortcut Commands with Alias in Linux 📂Programing

How to Set Up Shortcut Commands with Alias in Linux

Description

Alias is a feature that assigns a short name to a long or frequently used command, allowing the original command to be executed by using just that name. It helps improve efficiency by shortening commands you enter repeatedly.

Code

Write alias followed by name='command'. Note that there must be no spaces around the equals sign =.

Code:

alias ll='ls -alF'

Now, typing ll will execute ls -alF.

Code:

ll

Output:

total 12
drwxr-xr-x 2 user user 4096 Jun 12 09:00 ./
drwxr-xr-x 5 user user 4096 Jun 12 08:55 ../
-rw-r--r-- 1 user user   23 Jun 12 09:00 memo.txt

You can also include options or combine multiple commands in an alias.

Code:

alias update='sudo apt update && sudo apt upgrade'

Checking configured aliases

If you run alias with no arguments, you can view the list of currently configured aliases.

Code:

alias

Output:

alias ll='ls -alF'
alias update='sudo apt update && sudo apt upgrade'

Unsetting aliases

Use the unalias command to remove a configured alias.

Code:

unalias ll

To remove all configured aliases at once, use the -a option.

Code:

unalias -a

Setting aliases permanently

Aliases set directly in the terminal as above are only valid for the current session and will disappear when you close the terminal. To use aliases permanently, add them to your shell configuration file ~/.bashrc (or ~/.zshrc if you’re using zsh).

Code:

echo "alias ll='ls -alF'" >> ~/.bashrc

After editing the configuration file, apply the changes to the current session immediately using the source command. They will be applied automatically when you open a new terminal.

Code:

source ~/.bashrc