Using Aliases in Linux to Make Life Easier

Print
by Rob Williams on May 26, 2010 in Software

Of all the commands available in the Linux command-line, one of the most under-appreciated is “alias”. It’s not just useful, it can make using the CLI less of a chore, and even improve efficiency. In this article, we’re taking a look at what it is, and provide numerous examples of how to use it to become a true CLI power user.

Page 1 – Introduction; Setting up an Alias

One of the most common arguments against Linux as a whole is that it’s an operating system that relies heavily on the command-line, and thus it’s not for the novice user. Years ago, I wouldn’t have disagreed to that being the case, because even if you didn’t need the command-line for regular use, it wasn’t exactly rare to require one to install a driver, or a piece of software.

Thankfully, the situation is far different today, as many current distributions have have taken out a lot of the need for a command-line, thanks both to their highly-effective hardware detection and robust repositories. Today, if you want a new application of any sort, it’s likely to be available to you with minimal effort. At worst, you’ll be required to type in the name of the app, and your password. Très simple.

Although many of today’s distributions have taken away a lot of the need for a command-line, it’d be unwise to assume that it no longer has any use. In fact, I’d argue that to be a true Linux power user, you need the command-line, because you can get certain tasks taken care of far quicker than within the desktop GUI. Not to mention, you can gain unbelievable flexibility, and quickly.

I’ve been using Linux on and off for about thirteen years, and full-time for over four, so I’ve had quite a bit of time to become accustomed to the command-line. In many ways, it’s now like second nature. But it wasn’t until just a couple of years ago that I learned of the “alias” command, one that can help you make your command-line life even easier, in many different ways. Think of an alias as a way to take an ultimate shortcut.

The alias utility shall create or redefine alias definitions or write the values of existing alias definitions to standard output. An alias definition provides a string value that shall replace a command name when it is encountered.

Just as the blurb from the man page states, alias is the method of creating your own personal shortcuts for commands and functions you need on a regular basis. A quick and simple example would be to turn a command like “ls -las” into “las”, so that whenever you type “las”, the real command to be executed would be “ls -las”. Overall, that example might seem overly simplistic, but believe it or not, if you use such a command often, then the small things can make a big difference in the long-run.

Setting Up an Alias

One of the easiest parts of using aliases is creating them, as all you need to do is type in the command, followed by your desired shortcut command and the output. The best way to show this process is by a real example:

techgage@localhost ~ $ alias tgrox=’echo You don’t need me to tell you that Techgage rocks!’
techgage@localhost ~ $ tgrox
You don’t need me to tell you that Techgage rocks!

In this case, I’m setting “tgrox” to output a simple quote to the command-line. This example is simple, but it should do well to explain the actual process. Within those single quotes (this is important, they can’t be double quotes) you could put a small command, or a large one. You can even cue a script if you want, such as “alias script=’sh ~/script.sh'”. Like most things configuration-wise in Linux, the sky’s the limit.

Using the alias command is the easiest way to create aliases, but another is to edit your shell’s respective script file, which is always under your user’s root folder (/home/techgage/, for example). Depending on the shell you’re using, the name of the file will be different. The most common shell is Bash, which means the configuration file will likely be .bashrc. If that file doesn’t exist, doing an “ls -las” on your home folder should help you spot the correct one. Finding out which shell you’re using is also a good help:

techgage@localhost ~ $ echo $SHELL
/bin/bash

There’s one important thing to note, though. When creating an alias with either the alias command or by editing your respective shell script file, those commands will only work while in a terminal which is running inside of your favorite desktop environment, be it GNOME, KDE, Xfce or what-have-you. For many, this is fine, but it does mean that the same aliases will not work when you change to a different virtual environment (via Ctrl + Alt + F#).

The solution here is to either go to the virtual environment and set up the aliases in the same manner you set the original ones. Or, you can edit them into the .bash_profile file, which is also hidden in your user folder. In my experience, .bashrc has only ever controlled aliases in the desktop environment, and .bashrc_profile handle them in environments where no desktop environment is active. If you run into any issues, consulting your distribution’s support file regarding aliases should clear things up.

So how about putting some aliases to good use? On the following page, we provide numerous examples to help you become an alias-wielding master.

Support our efforts! With ad revenue at an all-time low for written websites, we're relying more than ever on reader support to help us continue putting so much effort into this type of content. You can support us by becoming a Patron, or by using our Amazon shopping affiliate links listed through our articles. Thanks for your support!

Rob Williams

Rob founded Techgage in 2005 to be an 'Advocate of the consumer', focusing on fair reviews and keeping people apprised of news in the tech world. Catering to both enthusiasts and businesses alike; from desktop gaming to professional workstations, and all the supporting software.

twitter icon facebook icon instagram icon