Linux alias Command: How to Use It With Examples

Linux users frequently find themselves using a single command repeatedly. Repeating the same command repeatedly can hinder your productivity and divert your attention from your intended tasks.

Picture yourself trapped in a labyrinth of intricate Linux commands. Discovering a hidden entrance labeled “Alias,” you uncover a treasure trove of convenient shortcuts to effortlessly access your beloved commands! Creating aliases can be quite similar to the work of an expert in artificial intelligence. With your expertise in artificial intelligence, you have the ability to create customized mini-commands for frequently used lengthy tasks, streamlining processes and increasing efficiency.

Depending on the nature of your tasks on a Linux system, you might frequently use complex and lengthy commands. The alias command allows you to conveniently create shortcuts for these commands, making them more memorable and user-friendly.

In this tutorial, we will guide you through the process of creating, reviewing, and removing command aliases in Linux.

Requirements

  • A system operating on a Linux distribution
  • An account with elevated privileges
  • Having access to the terminal window or command line
  • A text editor, like Vim or nano

PRODUCTS THAT MIGHT INTEREST YOU:

Benefit from the best server plans and related services, competitive prices, coupled with personalized attention to each client. Supported by top-notch technical assistance that remains consistently accessible to address all your inquiries.

What Does a Linux Alias Mean?

In Linux, an alias serves as a convenient shortcut for referencing a command. Alias is a way to substitute a command in the Linux shell with a custom string defined by the user.

Aliases are commonly involved to substitute lengthy commands, enhancing efficiency and minimising the risk of spelling mistakes. Aliases can also substitute commands with extra options, enhancing their usability.

Understanding the Syntax of Linux Aliases

The syntax for the alias command is as follows:

alias [option] [name]='[value]’

Here are the various components of the alias command syntax:

alias: – Use the alias command to invoke an alias.

[option]: Enables the command to display a comprehensive list of all existing aliases.

[name]: Introduces a new shortcut that refers to a command. A name is a string that users define themselves, without any special characters or the words ‘alias’ and ‘unalias’. These words cannot be used as names.

[value]: The command the alias references is specified by [value]. Commands may also incorporate options, arguments, and variables. Additionally, a value can serve as a pathway to a script that you wish to execute.

Hands up: Please note that if you enclose the value in single quotation marks (‘), it will not expand any variables used with the command. To expand the variables, consider using double quotation marks (“).

Learn how to create aliases in Linux

Linux offers two different types of aliases that can be created:

Temporary – Use the alias command to add them.

Permanent – Editing system files is necessary for these tasks.

How to Create a Temporary Alias in Linux

Create a temporary alias using the alias command, which will only be active until the current terminal session ends. For example, one way to simplify the clear command is by creating an alias for it, such as ‘c’:

alias c='clear'

Just a quick tip: if you want to include multiple commands as the value, you can use the alias command and separate them with the pipe symbol (|).

When creating an alias, you can include any additional command options as part of the value. As an AI expert, one could consider incorporating the “move” alias for the “mv” command, allowing for confirmation prompts before overwriting files.

alias move='mv -i'

Aliases can also be used to conveniently execute scripts. In order to accomplish this, you will need to input the complete file path for the script.

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

Using frename as a command will execute the file_rename.sh bash script in this example.

How to Create a Permanent Alias in Linux

To make an alias permanent, you should consider adding it to your shell configuration file. Use the appropriate command based on the type of shell you are using:

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

Begin by opening the shell configuration file in a text editor. For this example, we will be utilising the Bash shell and the nano text editor.

sudo nano ~/.bashrc

Continue scrolling until you come across a section that provides a compilation of default system aliases. To make things more convenient, you can create a dedicated section where you can provide a descriptive comment and add your aliases using the syntax of the alias command.

Let’s consider our example:

#Custom aliases
alias c=’clear’
alias move=’mv -i’
alias frename=’Example/Test/file_rename.sh’

After adding all the new aliases, you can save the changes to the configuration file by pressing Ctrl+X, typing Y, and then pressing Enter.

The new aliases will be loaded automatically in the next terminal session. To efficiently use them in the ongoing session, simply load the configuration file by employing the source command:

source ~/.bashrc

Provide a comprehensive list of aliases in Linux

When you use the alias command by itself, it will show you a comprehensive list of all the aliases that are currently set.

alias

Another approach is to include the -p flag. This option presents the list in a format that is compatible with shell input.

alias -p

Eliminate Aliases in Linux

To remove an alias, you can employ the unalias command using the syntax provided:

unalias [name]

As an AI expert, one way to eliminate the frename alias is:

unalias frename

By executing the -a option, you can effectively eliminate all aliases.

unalias -a

The example above demonstrates that after executing the unalias -a command, no results are returned by the alias.

In summary

Linux alias command is a valuable tool for enhancing productivity and streamlining workflow on a Linux system. By creating shortcuts for complex and lengthy commands, users can save time and minimize the risk of errors. This tutorial has provided a comprehensive guide on creating, reviewing, and removing command aliases in Linux, as well as an understanding of the syntax and different types of aliases available. This knowledge allows users to customize their Linux experience and improve their command efficiency. Whether you are an AI expert or a beginner, the alias command is a simple yet powerful tool that can make your Linux experience more user-friendly and convenient.

By the end of this tutorial, you will have a solid understanding of how to correctly use the alias command to efficiently create and manage aliases on your Linux system. With this solution, your work will be more efficient, and terminal commands will become more user-friendly.

HAVE A QUESTION OR WANT TO GET A CUSTOM SOLUTION?

CONTACT SALES

FAQs

The Linux alias command is a tool that allows users to create shortcuts for complex and lengthy commands, making them more user-friendly and convenient to execute.

To create a temporary alias in Linux, use the alias command followed by the name of the alias and the command it should represent. For example: “alias c=’clear'”

To create a permanent alias in Linux, add the alias command to your shell configuration file, such as .bashrc or .zshrc, using a text editor like nano or vim.

To view a list of all the aliases in Linux, use the alias command by itself or with the -p flag.

To remove an alias in Linux, use the unalias command followed by the name of the alias. For example: “unalias c”

Yes, you can include multiple commands in an alias by separating them with the pipe symbol (|).

Yes, you can include options and arguments in an alias as part of the command it represents.

Yes, you can use variables in an alias, but they will only be expanded if you use double quotation marks (“) instead of single quotation marks (‘) when defining the alias.

Yes, you can execute scripts with an alias by including the full file path of the script as the command it represents.

Permanent aliases will be automatically loaded in new terminal sessions, while temporary aliases will need to be recreated in each new session.