How to Use the Linux Command alias and 3 Tips

The Linux Command alias is an extremely useful tool for maximizing efficiency in your tasks. In this post, we’ll explore how to use the Linux Command alias and its various options.

What is the Linux Command alias?

An alias in Linux is a command that allows you to create a short, memorable name for a longer command or a frequently used command. For example, you can assign the alias ll to the command ls -alF to simplify the input.

Basic Usage

The basic format for using the alias command is as follows:

alias alias-name='commands'
ShellScript

For example, to shorten the command ls -alF to ll, you can set it up like this:

alias ll='ls -alF'
ShellScript

Now, when you type ll in the terminal, it will execute the ls -alF command.

Checking Alias Settings

Viewing All Aliases

To view all currently set aliases, simply enter the alias command:

alias
ShellScript

In the figure below, you can see all the aliases that have been set at once.

Figure 1. Viewing all set aliases in the Linux Command alias
Figure 1. Viewing all set aliases in the Linux Command alias

However, if you filter out only the aliases you are looking for, you can find the desired result more quickly.

Filtering Desired Results from All Aliases (feat. grep)

You can use the grep command to find specific results among all aliases. The following figure shows the result filtered by the string ls:

Figure 2. Filtering desired aliases using grep in the Linux Command alias
Figure 2. Filtering desired aliases using grep in the Linux Command alias

Checking a Specific Alias

You can use the type command to see the message “ll is aliased to ‘ls -alF'”, confirming that ll is an alias for the ls -alF command:

Figure 3. Checking a specific alias using the Linux Command type
Figure 3. Checking a specific alias using the Linux Command type

Removing an Alias

To remove an alias that you no longer need, use the unalias command:

unalias alias-name
ShellScript

For example, in the figure below, we have removed the alias ll. You can verify that the alias is no longer set using the type command.

Figure 4. Confirming the removal of an alias using the Linux Command type
Figure 4. Confirming the removal of an alias using the Linux Command type

To remove all aliases, you can use:

unalias -a
ShellScript

Setting Aliases Permanently

By default, aliases set with the alias command are only valid for the current shell session. They disappear once you close and reopen the terminal. To make an alias permanent, you need to add it to the shell configuration file in your home directory.

For Bash Shell

Open the .bashrc file in your home directory:

nano ~/.bashrc
ShellScript

Add the alias setting at the bottom of the file, or, as shown in the figure below, add it in a section where ls command aliases are grouped for easier management:

Figure 5. Setting an alias in the ~/.bashrc file
Figure 5. Setting an alias in the ~/.bashrc file

Save the file by pressing Ctrl + O, then exit the editor by pressing Ctrl + X.

Apply the settings by running:

source ~/.bashrc
ShellScript

For Zsh Shell

If you are using Zsh, the configuration file is .zshrc. The process is similar to Bash:

Open the .zshrc file in your home directory using the nano editor:

nano ~/.zshrc
ShellScript

Add the alias setting, and for better management, comment and separate the section where you add aliases. If there is already an alias setting section, edit it; if not, add the desired alias at the bottom of the file as shown below:

# My Custom alias
alias ll='ls -alF'
ShellScript

Save the file and exit the editor.

Apply the settings by running:

source ~/.zshrc
ShellScript

Useful Tips for Using Aliases

Using aliases can simplify repetitive tasks. Here are some useful alias settings:

Directory Navigation

You can set an alias to quickly navigate to frequently used directories. For example, the following alias setting allows you to type proj to move to the ~/Projects directory:

alias proj='cd ~/Projects'
ShellScript

System Update

Updating the Ubuntu system requires a long and complex command. You can simplify it as follows, allowing you to type update to run the system update command:

alias update='sudo apt update && sudo apt upgrade -y'
ShellScript

Network Check

You can simplify the command to check your IP address with an alias. The following setting allows you to type myip to check your external IP address:

alias myip='curl ifconfig.me'
ShellScript

Cautions

There are a few things to keep in mind when using aliases:

Avoid Duplicates

Creating an alias that is the same as an existing command can cause confusion. Make sure the alias does not overlap with commands you already use. When setting aliases in files like .bashrc or .zshrc, gather them in one section to avoid duplication.

Security

Be cautious when creating aliases for certain commands, especially those involving sudo. Incorrect settings can impact the system.

Set Appropriately for the Situation

Not all alias settings are useful in every situation. Adjust the settings to match your frequently used environment.

Summary

The Linux Command alias is a very useful tool for Linux users. It can shorten long commands to increase efficiency and simplify tasks. By adding aliases to the configuration file, you can use them permanently, and various utilization methods can greatly enhance productivity.

I hope this post has helped you understand the basic usage and various options of the Linux Command alias, and that you can use it effectively in your actual environment. Continue to explore and use more useful tools and commands in the Linux environment for efficient work.

References

Leave a Comment