Linux > Aliases

Aliases can be a great way to more easily remember an oddball command or to shorten a long command. Aliases are stored in your .profile (or .bash_profile) or within a include file referenced from .profile.

A couple of quick examples before showing how to set them up:Original Command:

Much faster to type in that alias isn’t it? This is especially useful when you have a command that you use all the time. First we’ll take a look at a simple alias.

Basic Aliases

alias tucows="whois -h whois.tucows.com"

Now instead of typing whois -h whois.tucows.com example.com every time I want to do a lookup referencing tucows I can simply type the following

tucows sudobash.net

This is clearly easier to remember as well as faster to type.

Conditional Aliases
Those aliases are great but what if you are running in an environment where you are hopping from server to server and your alias breaks? For instance, on one server you may be using grep to include color (grep –color) but on other servers this option is not present and every time you grep you will receive an error message complaining about how –color isn’t a valid option.
Note: This is only an issue if you are using a mounted home directory that will use the same .profile across multiple servers. If you don’t do that then you’ll have to setup your aliases individually on each server (a pain for large environments).

This demonstrates that our alias would work on one server but not on another (In this case it doesn’t matter – Ubuntu can have color grep by default, we don’t have to tell it to use ggrep).

So how do we fix this so that we can use grep in both locations with both of them working? On this particular example we’ll check if ggrep is actually installed (and where we expect it to be) and if it is then use the alias.

Ok, so we’ve seen the test examples. Where on earth does all this stuff go? As previously mentioned just paste and edit your code in the .profile file in your home directory OR you can have a separate file named whatever you like (I recommend .bash_aliases) and then reference that file in your .profile (this makes for a less messy .profile)