Sep 19, 2023 4 min read

How to Create and Use Alias Command in Linux

Create and Use Alias Commands in Linux with our step-by-step tutorial.Alias is a short name that represents a longer command or set of commands.

Create and Use Alias Command in Linux
Table of Contents

Before we begin talking about how to use alias command, let's briefly understand - What is alias?

Creating and using alias commands in Linux provides a way to customize your command-line interface and enhance productivity. An alias is a short name or abbreviation that represents a longer command or set of commands. By defining aliases, you can save time and typing effort, as well as create shortcuts for frequently used commands.

One command is often used repeatedly by Linux users. Having to type or copy the same command repeatedly takes up time and distracts you from what you are really doing.

In this tutorial, you will create and use the Alias command in Linux. We will also address a few FAQs on create and use alias command in Linux.

List Currently Defined Aliases in Linux

A list of defined aliases can be found on your profile by simply executing the alias command.

alias

An alias can be created by simply using a single character that represents the command of your choice.

How to Create Aliases in Linux

The process of creating aliases is relatively easy and quick. You can create both temporary and permanent aliases. We will discuss both types in this tutorial.

Create Temporary Aliases

To create an alias for a command, type the word alias, the command you want to use, a "=" symbol, and the command you want to alias.

Follow the below syntax:

alias shortName="your custom command here"

Check the below example:

alias wr="cd /var/www/html"

If you want to go to the web root directory, simply use the wr shortcut. Unfortunately, you will only be able to use that alias in the current session.

You will no longer be able to use the alias if you open a new terminal session. If you start a new terminal session, you won't be able to utilize the alias.

Create Permanent Alias

You can save aliases in your user's shell configuration profile file to maintain them between sessions. This might be anything from:

  • Bash – ~/.bashrc
  • ZSH – ~/.zshrc
  • Fish – ~/.config/fish/config.fish

The syntax is nearly identical to that of a temporary alias. The only difference is that this time you'll be storing it as a file. For instance, in bash, you can open the .bashrc file with your preferred editor as follows:

vim ~/.bashrc

Find a location in the file to save the aliases. You may, for example, include them towards the end of the document. For organizational purposes, you can write a comment like this before your aliases:

#My custom aliases
alias home="ssh -i ~/.ssh/mykep.pem [email protected]"
alias ll="ls -alF"

The file should be saved. In your next session, the file will be loaded automatically. Run the following command if you wish to use the newly defined alias in the current session:

source ~/.bashrc

The unalias command can be used to delete an alias added via the command line.

unalias alias_name
unalias -a [remove all alias]

FAQs to Create and Use Alias Command in Linux

How do I make aliases persistent across sessions? 

To make aliases persistent, add them to a configuration file such as .bashrc or .bash_aliases in your home directory. These files are automatically loaded when you start a new terminal session.

Can I override existing commands with aliases?

Yes, you can create aliases that override existing commands. However, exercise caution when doing this, as it may lead to confusion or unintended consequences.

Can I use arguments with aliases? 

Yes, you can use arguments with aliases. Using the special characters $1, $2, etc., you can pass arguments to the commands associated with the alias. For example, alias c="cd $1" will create an alias "c" that changes to the directory specified as an argument.

How can I list all the aliases I have created?

To list all aliases defined in your current session, simply type alias without any arguments. It will display a list of aliases and their respective commands.

Can I use aliases with command-line options or flags? 

Yes, you can use aliases with command-line options or flags. Simply add them after the alias name. For example, if you have an alias "c" defined as alias c="clear", you can use c -v to clear the screen and show verbose output.

Can aliases include pipes, redirects, or other advanced shell features? 

Yes, aliases can include pipes, redirects, and other advanced shell features. However, in such cases, it is recommended to use a function instead of an alias for more complex command sequences.

How can I share my aliases with other users or systems? 

To share your aliases with other users or systems, you can add them to system-wide configuration files such as /etc/profile or create a shared configuration file that is sourced by multiple users.

Conclusion

Creating and using alias commands in Linux is a powerful way to customize your command-line environment. By defining aliases, you can save time, avoid repetitive typing, and create shortcuts for complex or frequently used commands.

Whether it's simplifying directory navigation, executing complex sequences of commands, or creating personalized shortcuts, using aliases can significantly boost your productivity and efficiency in Linux.

By following the steps outlined in this tutorial and exploring further possibilities, you can optimize your Linux command-line experience and make it tailored to your specific needs and preferences.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.