Using Bash Aliases

Bash aliases are a very useful feature for UNIX command line users. In this article I will discuss what they are and how to take advantage of them.

Aliases are a way to say “when I type this, I want you to interpret it as this other thing.” For example:

alias ft='find . -type f'

If you type ‘ft’ at the command line before entering this alias command, you will most likely get:

-bash: ft: command not found

Once you type the alias command in, typing ‘ft’ should start firing off a list of all files recursively under your current directory. You now have a way to enter the longer command by simply typing 2 characters. This can come in handy when there are strings of commands that you use frequently that are long and/or hard to remember.

So now that we understand what an alias is, let’s make it so that every time we open a new terminal window we setup some aliases automatically. This can be done by putting the alias directly in the .bashrc files in your home directory, but I prefer to create a .bash_aliases file and reference that from the .bashrc file. This helps me keep things organized and makes it easier to share aliases across multiple machines. So under your home directory use your favorite text editor to create (or open) a file called .bash_aliases. Add some aliases like:

~/.bash_aliases file:

alias tu='top -o cpu' #top cpu processes
alias tm='top -o vsize' #top memory processes
alias ls='ls -FG'

Notice how I create an alias for an existing command ‘ls’. This allows me to set my preferred defaults so that I can just use ‘ls’ and get the enhanced command instead. Now we need to add a couple of lines to our .bashrc file so that it knows to pull in the aliases from our .bash_aliases file.

~/.bashrc file:

if [ -f ~/.bash_aliases ]; then source ~/.bash_aliases; fi

Adding this line to your .bashrc tells bash that when you start a terminal to check your home directory for a file named .bash_aliases and include it as a configuration file.

That is it. Now when you find a need for a new command shortcut you can simply add it to your .bash_aliases file and it will be available anytime you start up a new shell. If you would like to see the list of aliases that you have, maybe that you've created or that were setup for you when your account was created, simply type:

alias

That should give you a full list of all currently existing shell aliases.

Hopefully this gives you the basic idea of creating and managing bash aliases. They can be a real benefit if you spend much time at the command line helping you reduce keystrokes and speed up repetitive processes.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>