4 Ways to Use the Linux alias Command

Try this guide with our instant dedicated server for as low as 40 Euros

linux alias command

Consider the following command that prints the top ten directories that take up the most disk space :

# du -h /path/to/directory | sort -hr | head -n 10

This is a frequent check, and you can imagine the effort required to type this command in the terminal and scripts. Repetitively typing or copying the same command lowers productivity and introduces a hassle you can easily avoid with the alias command.

By making aliases for the commands you use frequently, you can save yourself a couple of hours every month. Similar to personalized shortcuts, aliases refer to a command (or group of commands) that can be used with or without personalized parameters.

You are likely utilizing aliases on your Linux system without being aware of the fact.

This article will introduce you to the alias command. We’ll show you how to create command aliases with the Linux alias command. Next, we’ll go into the details of making command aliases permanent on your system. Finally, we’ll show you how to remove command aliases.

But first, let’s start with a short introduction to the idea of command aliases in Linux.

Table Of Contents

  1. What Is an Alias in Linux?
    1. Prerequisites To Working With the Linux alias Command
  2. How To Use Linux alias Command
  3. List the Currently Defined Linux Aliases
  4. Create Aliases in Linux
    1. Create a Temporary Alias
    2. Create a Permanent Alias
  5. Remove Aliases in Linux
  6. Conclusion
  7. FAQs

What Is an Alias in Linux?

In Linux, an alias is a shortcut that leads to a command. In technical terms, an alias is a user-defined string that replaces another string (consisting of commands and parameters) when called by a command in the Linux shell.

Aliases are usually used in place of long commands to improve productivity and lower the possibility of typing errors. Users can use long commands without having to remember them. When defined, the user enters the alias, and the shell replaces it with the actual command.

Prerequisites To Working With the Linux alias Command

Before working with the Linux alias command, you will need the following:

  • A computer running a mainstream Linux distribution
  • An account that has sudo access
  • Command line or terminal access
  • Your preferred text editor

How To Use Linux alias Command

The idea of using aliases for commands is not restricted to power users. Novice users can also set up aliases for frequently used commands.

However, before going into the details of setting up command aliases, let’s look at some aliases that are usually available by default on a Linux system.

List the Currently Defined Linux Aliases

You can view a list of aliases present on your profile using the alias command.

# alias

The output shows the default aliases available for the logged-in user on our test Ubuntu system.

Defined alias

For instance, you can see that the command alias ll is used for the ls -alF command.

We recommend using the -p flag with the alias command so that the output is presented in a format where you can copy the command and reuse it:

# alias -p

alias -p

Create Aliases in Linux

You can set up a single character to create an alias that will function as the paired command. Here are the two types of aliases in Linux:

  • Temporary: Use the alias command to create these aliases.
  • Permanent: Edit system files to add permanent command aliases.

Create a Temporary Alias

Use the alias command to make a temporary command alias that is removed when the current terminal session ends. For instance, here’s the command to substitute the letter c as an alias for the clear command:

# alias c='clear'

You can add command arguments and parameters when creating an alias. This is a great way to add the commonly used flags to a command. For instance, the following command sets move as an alias for the mv command and add the -i flag to enable a request for authorization before overwriting:

# alias move='mv -i'

A common use of temporary aliases is creating a shortcut for executing scripts. For this, assign the script’s path to an alias. For instance, consider the following command:

# alias frename='Example/Test/file_rename.sh'

After executing this example command, you can use the alias frename to run the file_rename.sh bash script.

Create a Permanent Alias

Temporary aliases disappear when you close the terminal session. This means you need to declare these aliases every time you start a new terminal session.

You can avoid this hassle by adding permanent aliases. For this, you can edit the shell configuration file and add an entry for the alias. Here are the configuration files for the popular shells:

  • Bash shell: ~/.bashrc
  • Zsh shell: ~/.zshrc
  • Fish shell: ~/.config/fish/config.fish

We will edit ~/.bashrc, the configuration file for the Bash shell in the Nano editor for this demonstration.

Start by running the following command in the terminal:

# sudo nano ~/.bashrc

permanent alias

Scroll down to the section for alias definition.

Add your aliases with a detailed comment. Remember to add each alias on a separate line.

In our example:

add alias

Save the configuration file with Ctrl+X and then press Y and Enter.

You need to restart the terminal session so that the system loads the updated config file. Alternatively, use the source command to load the configuration file if you wish to use the aliases in the current session:

# source ~/.bashrc

Remove Aliases in Linux

You can use the unalias command to remove a previously set command alias. The syntax of the command is as follows:

# unalias [name]

To demonstrate the command, we will use the following command to remove the alias frename we set in a previous section.

# unalias frename

remove alias

If you wish to unset all command aliases, use the -a flag with the command:

# unalias -a

unalias -a

As you can see, the unalias -a command removed all command aliases from the system.

Conclusion

The Linux alias command is a valuable tool for personalizing and organizing tasks that require the command line. It boosts your productivity and reduces time spent on tasks like personalizing shortcuts and simplifying complicated commands.

We presented examples that highlight the adaptability and practical applications of aliases. Gaining proficiency with the alias command allows Linux users to customize their terminal window environment, resulting in a more effective and customized workflow. People can fully utilize this feature by experimenting with different configurations.

Are you prepared to take full advantage of the advantages of dedicated hosting? With the help of RedSwitches, your worldwide dedicated hosting provider, unlock all the possibilities of your Linux command-line interface. Simplify work, reduce time, and boost productivity.

We offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need dedicated servers, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. What is the Linux alias command?
Use the Linux alias command to make unique custom shortcuts or symbols for more extended original commands. It enables users to boost productivity and streamline command-line operations.

Q. How do I create an alias in Linux?
The following syntax should be used to create an alias: alias command = alias_name. For instance, the command ls -la can be referred from the alias ll’ when the command alias ll=’ls -la’ is used.

Q. Can I use variables in Linux aliases?
You can use variables in Linux aliases. For instance, alias greet=’echo Hello, $USER’ will greet you using your username.

Q. How can I make aliases permanent?
Add aliases to your shell configuration file (such as .bashrc,.bash_profile, or .zshrc) to make them permanent. This ensures that they are loaded each time you launch a new shell current session.

Q. Can I chain multiple commands in a single alias?
You can chain multiple commands in a single alias by separating them with semicolons. For example, alias update=’sudo apt update && sudo apt upgrade’ updates your system in one command.

Q. How do I list all existing aliases?
Use the alias command without arguments to see a list of all currently used aliases. A list of the aliases currently defined in your terminal current session will appear.

Q. Can I remove an alias?
You can use the unalias command and the alias name to remove an alias. To remove the ll alias, for instance, use unalias ll.

Q. Are aliases supported in all Linux distributions?
Yes, the alias command is a standard feature in most Linux distributions and is supported across various shells, including Bash and Zsh.

Q. In what ways can aliases improve workflow efficiency?
Aliases contribute to a more efficient workflow by reducing the need for repetitive typing, making complex commands more accessible, and enhancing overall productivity in the Linux command line.

Q. Where can I find more examples of Linux aliases?
The article provides examples, but you can explore online resources, forums, and Linux documentation for various creative and useful alias configurations tailored to different needs.

Try this guide with our instant dedicated server for as low as 40 Euros