Introduction to Bash Aliases

Spread the love

In this article, we will show you how to make use of the alias command through some useful examples. We’ll cover creating aliases, show you a few useful examples, and show you how to delete aliases as well.

If you are working on the command line a lot of the time, you may have noticed that most of the commands you use are extended commands with flags, or options, along with piping the output of commands into other programs.

Imagine that every time you needed to change directory, you would have to type “change-directory” instead of “cd” or if every time you needed to list the contents of a directory, you’d have to type “list” instead of “ls“. That’s why the makers of most commands attempt to eliminate the long names of the commands and replace them with shortened names. It reduces the amount of typing you have to do to get things done, and also makes the commands easier to remember as a bonus, thanks to the shorter name.

Basically, bash aliases are just shortcuts for the longer commands. The alias command allows us to run any command or even a group of commands (which includes options and filenames) by typing a custom phrase (which can even be just a single word) into the command line.

How to List Bash Aliases

You can list the bash aliases by typing alias on the command line.

alias

The output may look like an example below.

alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias sl="ls"
alias apt='sudo apt'

How to Create a Bash Alias

There are two types of declaring a bash alias.

All user-defined aliases should go into the ~/.bashrc file – this way they will persist through restarts as well as logging out and back in. You can run them in the command line interface, but the aliases will only apply in your current bash session.

You can use the following syntax to create a bash alias:

alias [alias_name]="[alias_command]"

When you want to add a new alias, it should always be on a new line and it needs to start with the alias keyword. After you type the word alias, you leave a blank space and define the alias name followed by an equal sign. Finally, you can set the full command after the equal sign between the quotation marks, as in the example shown above.

You need to edit the .bashrc file if you wish to create permanent aliases. Go to the end of the file and add the aliases to a new line. You can also add a comment so you can easily see where all the aliases are. For example:

#############
# Aliases
#############

alias df="df -h"

Before you add new aliases, you can always check that they have already been added from your Linux distribution, since many of the distributions come with a configuration with already added aliases. After you have added the aliases you can save and close the file.

All new aliases when added will be available for use the next time you start a new shell session. However, if you want to view the changes without doing this, you can run the following command so that bash will read the file again in your current session:

source ~/.bashrc

Useful Alias Examples

Since we know how to create our own aliases, now is when we will show you some popular aliases that can make your typing and system administration experience a little nicer. We can start with some simple aliases for navigation.

The most common command that is used for listing is ‘ll‘ alias. With this alias, you can list all the files along with the hidden files in a human-readable size.

alias ll="ls -lahF"

If you want a short version of navigation to different directories you can add this:

alias ..='cd ..'
alias ...='cd ../..'

You can use the process status command so it can display more information about the active processes:

alias ps="ps -aux"

Another useful alias for displaying statistics about the amount of free disk space is the ‘df‘ command. This alias displays the sizes in a more human-readable format:

alias df='df -h'

You can easily find files in the current directory by setting this alias:

alias fhere="find . -name "

Temporarily Stop Using Aliases

There are situations that you need to use the actual command instead of the alias that already exists. But you do not want to remove the alias and then define it again. Sounds annoying right? That’s why you can use a small trick that will allow you to temporarily use the original command.

\aliasname

For example, alias cp=“cp -iv”, will constantly be asking about confirmation to overwrite the file. Imagine that you need to copy a lot of files and you know that all the files need to be overwritten. In this case, you might want to use the regular cp command. You can see a small example shown below:

\cp * /backup/directory/

Removing an Alias

As the last part of this article, we will show you the opposite of adding which is removing and unsetting aliases.

You can remove/unset aliases with the following command:

unalias ll

ll

-bash: ll: command not found

If you want to remove/unset all set aliases, you can just type:

unalias -a

We hope that with this article you now have an idea on how to create your own aliases and maximize your command line efficiency. Using aliases you can make your time in the shell more enjoyable – not to mention faster.


Of course, you don’t have to create bash aliases if you use one of our Fully Managed VPS Support Services, in which case you can simply ask our expert Linux admins to create bash aliases for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked our post on an intro to bash aliases, please share it with your friends on the social networks using the buttons, or simply leave a reply below with your favorite bash aliases. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *