Linux alias Command: How to Use It with Examples

June 6, 2024

Introduction

Advanced operations in a Linux system frequently require entering long and complicated command strings. The alias command allows the user to create replacements for other commands and make them easier to remember and use.

This tutorial shows you how to create, review, and remove command aliases in Linux.

Linux alias command: How to use it with examples.

Prerequisites

What Is Alias in Linux?

An alias is a replacement string that references a Linux command and produces the same results as when executing the original command. It can be user-defined or predefined by the system or an application.

The most common purpose of an alias is to replace a long command or an expression containing multiple options and arguments. Using aliases in everyday work can improve efficiency and prevent potential syntax errors.

How Does alias Command in Linux Work?

When a user executes an alias, Linux does not recognize it as a valid command and searches the memory for the previously created alias definition. An alias definition is a statement (entered via CLI or placed in a shell configuration file) that connects the alias and the original command it represents.

If the definition is found, the system replaces the alias from the user input with the original command and executes it.

Linux alias Syntax

Aliases in Linux are created with the alias command. The command uses the following syntax:

alias [option] [alias]='[command-or-path]'

The list below defines each of the alias command elements:

  • alias - Invokes the alias command.
  • [option] - Allows additional command options, such as listing all active aliases.
  • [alias] - Defines a new string that references an existing command. The name must exclude special characters and the words alias and unalias, which cannot be used as aliases.
  • [command-or-path] - Specifies the command or the path to a script that the alias references. Single quotes separate this element, and it can include options, arguments, and variables.

Note: If the original command contains a variable, enclosing it in single quotation marks (') does not expand the variable's value, i.e., the string $variable is passed literally. To pass the value, surround the variable with double quotation marks, i.e., "$variable".

Linux alias Options

The alias command features only two options that extend its functionality:

  • -p - Outputs a list of all the active aliases on the system in a reusable format.
  • --help - Prints help.

How to Create Aliases in Linux

There are two types of aliases in Linux:

  • Temporary. The aliases added via CLI, using the alias command. Temporary aliases disappear after the user exits the terminal.
  • Permanent. The aliases saved in a shell configuration file on the system. Permanent aliases persist across terminal sessions.

The following sections explain how to create temporary and permanent aliases.

Create Temporary Alias in Linux

Use the alias command to create a temporary alias that lasts until the end of the current terminal session. For instance, type the following command to create print as an alias for the echo command:

alias print='echo'

The print alias can now be used interchangeably with echo, as in the following example:

print "test"
Using the print alias to execute the echo command.

Note: Include multiple commands as the value in a single alias by separating them with a semicolon (;). For example, alias test='clear; ls'.

To reference any additional command options when creating an alias, include them as a part of the value. The example below creates list as an alias for the ls command with options to show directories and files in a list format and include hidden items:

alias list='ls -la'
Executing an alias for the ls command with additional options.

An alias can also serve as a shortcut for executing a script. Create a script alias by providing the absolute path to the script as the value:

alias [alias]='[path-to-script]/[filename].sh'

For example, type the command below to create frename as an alias for the file_rename.sh bash script located in the user's home directory:

alias frename='/home/[user]/file_rename.sh'

Create Permanent Alias in Linux

To make an alias permanent, add it to the shell configuration file. Different shells store configuration in different files. Below is the list of the configuration files for the most popular types of shells:

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

Note: Some shells allow storing aliases in a separate file called .aliases.

Proceed with the steps below to create a permanent alias:

1. Open the shell configuration file in a text editor such as Nano. The example below opens the Bash shell configuration file (.bashrc):

sudo nano ~/.bashrc

2. Find the section that lists default system aliases.

3. Create a separate section below the default section. Include a descriptive comment and add your aliases using the alias command syntax.

For example, to add the aliases previously created in this tutorial, type:

#Custom aliases
alias print='echo'
alias list='ls -la'
alias frename='home/[user]/file_rename.sh'
Editing the .bashrc file to include custom aliases.

Save the file and exit. The new aliases automatically load in the next terminal session.

4. If you want to use the new aliases in the current session, load the configuration file using the source command:

source ~/.bashrc

List All Aliases in Linux

To list all available aliases on the system, execute the alias command without any arguments:

alias
Listing all current aliases in Linux

Alternatively, use the -p flag to achieve the same result:

alias -p

Both commands print outputs suitable for reuse in a shell.

Remove Aliases in Linux

To remove an alias, use the unalias command with the following syntax:

unalias [alias]

For instance, to remove the frename alias, enter the following:

unalias frename

Adding the -a option allows you to remove all aliases:

unalias -a

Examples of Common Aliases

Aliases can shorten the commands, simplify procedures, and provide additional security for the system. Below is a list of aliases frequently used on Linux:

  • Replace the clear command with c to clear the screen more efficiently:
alias c='clear'
  • Create a set of aliases for the cd command to simplify moving down through the directory tree:
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
  • Modify the mkdir command to ensure the system creates parent directories if they do not exist. The -p option creates directories while -v provides verbose output:
alias mkdir='mkdir -pv'

Note: If you replace the command with an alias of the same name, executing it will always execute the alias. To ignore the alias and use the original command, type its full path, e.g., /usr/bin/mkdir.

  • Shorten the repository update and upgrade commands and execute them with a single alias:
alias update='sudo apt-get update && sudo apt-get upgrade -y'
  • Ensure that the mv command does not replace a file without asking for confirmation:
alias mv='mv -i'

Conclusion

After reading this tutorial, you should be able to use the alias command to create and manage aliases on your Linux system. Aliases will help streamline your work and make terminal commands simpler to use.

To learn more about other commands in Linux, check out our Linux commands cheat sheet.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Use the Linux watch Command with Examples
August 11, 2021

The built-in Linux watch command allows you to repeatedly run a user-defined command in regular time intervals. Learn how to use it in this tutorial.
Read more
Linux Source Command with Examples
July 22, 2021

The Linux source command allows you to execute a list of commands from a text file or script. Learn the different ways you can use the source command in this tutorial.
Read more
How to Use the wall Command in Linux
February 18, 2021

The wall command allows sending terminal messages to all logged-in users at once. This feature is particularly useful in scenarios when system administrators perform system maintenance tasks.
Read more
How to Use the Linux tee Command
January 28, 2021

By default in Linux, a command received through standard input writes directly to standard output. The tee command changes that behavior by allowing another command to use a file as the output.
Read more