Introduction

This tutorial covers creating, deleting and persisting aliases.

If you find yourself typing the same command over and over again, you can create a shortcut for it, called alias. An alias can be thought of as a text expander. Creating aliases for commands that are really long, or that you use often is a common practice.

For example, if you type ls -l frequently, you may want to abbreviate it to ll. As a matter of fact, this alias often comes predefined in many Linux distributions.

Creating Aliases

The alias command can list or create aliases. If you run alias without any additional information on the command line, it displays the list of current aliases that are set.

If you want to create an alias use:

alias aliasname='commands'
Example 1 - Typo

Aliases don't have to be used for just shortcuts. They can be used to fix common typing errors.

If you end up typing grpe when you mean to type grep, you can create an alias

$ alias grpe='grep'

Then, whenever you accidentally type grpe, alias will fix that for you, and spell it grep.

Example 2 - Work Environment

You can also use aliases to make your work environment similar to another platform.

For example, in Windows cls clears the screen, but in Linux the equivalent command is clear. You can create a shortcut to make you feel more like Windows in Linux system.

$ alias cls='clear' 

Removing Aliases

To remove an alias type unalias aliasname, or to remove all alias use unalias -a.

Persisting Aliases

If you have created some aliases and logged out, your aliases would be lost. To make them persist between sessions, add them to your dot files, such as .bash_profile.

The downside to create several aliases is that when you are on a system that does not have your aliases you might feel lost. If you want to be able to work effectively on any Linux system that you have access to, keep your alias usage to a minimum.

Another way to handle this situation is to simply copy your configuration files to each Linux system that you are going to work on.

Examples

Example 3 - ll

Lets say you run ls -l a lot. Let's make this an alias, make it shorter. Let's

$ alias ll='ls -l'

So now when you type ll, you'll get the same result as when you type ls -l.

Example 4 - cls

Let's say you like to type cls because you come from a Windows background. You can create an alias for cls that equals clear.

$ alias cls='clear'
Example 5 - List alias

To list alias, just type alias command.

$ alias
alias ll='ls -l'
alias cls='clear'
Example 6 - Make alias persist

To make alias persist, you can edit your bash_profile file.

$ vi ~/.bash_profile
# .bash_profile
 
# User specific environment and startup programs
 
PATH=$PATH:$HOME/bin
 
export PATH
 
# My alias
alias cls='clear'
alias ll='ls -l'
~
~
~