How to Create an Alias in Linux

Use aliases as shortcuts for Linux commands

Despite the advances of the graphical desktop for Linux, you can't escape the shell. If you manage a Linux-based server, the shell is generally all you get. To make the issuance of common but potentially cumbersome commands easier, Linux includes an alias utility to offer shortcuts for these commands.

How a Linux Alias Works

The alias command creates a series of entries in the configuration file of the current shell. For example, in ~/.bashrc for Bash or ~/.zshrc for Zsh.

Although you can edit aliases in a shell's configuration files, use alias to avoid mistyping something that could cause errors in the shell. Veteran Linux users install two shells, for example, Bash and Zsh, or Csh, or Tcsh. Then, if the config files for one shell are corrupted, you can create a new terminal session with the other shell to facilitate repairs.

Executed on its own, alias lists the active aliases for the shell. Because the alias parameters are shell-specific, if you routinely use more than one shell, you need to repeat your aliases in others.

When you execute a stored alias, the shell expands the original content in place of the alias shortcode. For most shells, including Bash, this functionality limits to command expansion. However, other shells, including Zsh, support a more robust system of global substitutions for aliased content.

How to Create an Alias in Linux

Create a new alias, regardless of the shell, with the following command:

alias shortcode="full_command_to_execute"

The shortcode represents the custom command you'll invoke from the shell prompt, and full_command_to_execute references the full command name. It's prudent to be explicit with the full command by including full pathnames instead of relative pathnames where relevant.

After you execute alias in this way, the command displays nothing unless you commit a syntax error. Assuming there are no errors, the newly aliased shortcode is ready to use.

For example, to append new lines to a diary file, a diary alias could invoke cat with an append-redirection to that diary file. For example, the command:

alias diary="cat >> ~/Documents/diary.txt"

creates a diary alias that, when executed, puts the shell into a text-edit mode. Add new content, then press Ctrl+D to save it. The cat command appends that text to the end of the diary.txt file stored in your home Documents folder.

linux alias

Change and Remove Aliases

To change an alias, redefine it. A new alias command using the same shortcode overwrites the full command of the original shortcode.

To remove an alias, use the unalias command, as follows:

unalias shortcode

Run alias to see what aliases are installed on your system. For example, people who favor Zsh and install the Oh-My-Zsh utilities automagically enjoy several standard aliases. Adding extensions to Oh-My-Zsh (for example, the git integration) can add up to 100 more. Don't assume that because you never added an alias, that your computer doesn't have any aliases.