Date: May 26, 2010
Author(s): Rob Williams
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.
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.
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:
[email protected] ~ $ alias tgrox=’echo You don’t need me to tell you that Techgage rocks!’
[email protected] ~ $ 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:
[email protected] ~ $ echo $SHELL
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.
Although this list may come off as being a top list of sorts, it isn’t. The goal here is to introduce the alias command to those who may still be unaware that it exists, while showing off real-world examples of how it could be used. Some of the examples are more than examples to me, though, as I use them on a day-to-day basis. It’s my hope that with these examples, you’ll come up with ideas of your own and help make your command-line life just a wee bit easier.
So, let’s get on with it!
Since the “df” command shows storage device sizes in kilobytes, I personally replace the command with another in order to have it always show in gigabytes instead, where it’s easier to understand:
alias disk=’df -BMB’
[email protected] ~ $ disk
Filesystem 1GB-blocks Used Available Use% Mounted on
/dev/sda9 61GB 32GB 26GB 56% /
Because I keep backups of all my important data not only on a NAS box but the same PC I’m using, I have a script that runs a couple of times a week in order to keep a backup folder on a second hard drive in sync with my regular documents. If I had wanted to do this with alias, that would also be simple:
alias docusync=’rsync -av /home/techgage/documents/ /mnt/backup/documents/’
[email protected] ~ $ docusync
sending incremental file list
When you’re doing your thing in a command-line, do you regularly visit the same folders over and over? An alias can take out some of the bite there, by allowing you to get to a certain folder with a small command, not an overly large one:
alias distfile=’cd /usr/portage/distfiles/’
[email protected] ~ $ distfile
[email protected] /usr/portage/distfiles $
As far as OSes go, Linux tends to be rather stable, but the same can’t always be said for some of the applications being run. If such an application decides to crash, sometimes it’s easier to open up a command-line to kill it before your desktop environment asks you to. One good way to do this is to use the “ps” command in order to find the application’s ID.
To filter through the output, a “| grep” extension must be used, along with the name you want to filter out. Admittedly, “ps ax | grep” is a little rough to type, but “process” isn’t:
alias process=’ps ax | grep’
[email protected] ~ $ process firefox
6078 ? SLl 429:24 /usr/bin/firefox
In that example, you can see that aliases don’t have to be a one off-command. Because an alias replaces a string, you can customize your input while using them. In this particular example, because I turned “ps ax | grep” into “process”, typing in “process firefox” was essentially the same as “ps ax | grep firefox”. See just how useful aliases can be? Well, we’re not done yet.
Run a server? Connect to it via SSH on a regular basis? Sick and tired of typing in both the string and password every time you need to connect? Boy, are you going to love aliases. First, to get past the password boundary, I recommend setting up an rsync-key on on both machines. To learn how to do this, you can refer to our article “Backing Up Your Linux” that explains it in full.
Once that is set up, you could make it so that connecting to your SSH server is as simple as typing in the domain name, or with something even shorter:
alias tg.com=’ssh -i /root/.ssh/rsync-key [email protected]’
localhost techgage # tg.com
Last login: Thu Apr 1 12:00:00 2010 from blah.blah.cable.provider.com
[[email protected] ~]#
Note: When creating an alias that allows major access to a remote server, it should be created as root, not as the regular user. If it’s created as the regular user, it could be considered to be a huge security hole due to the ease of access.
Are you one of those who either has to, or prefers to, update your Linux via the command-line? As I’m a Gentoo user, the best course-of-action for me is to run a command like “emerge –sync” and then “emerge -avuD world” immediately afterward in order to both sync the repositories and then cue the update. Oh, and just to add a little more into the mix, I use a tool called eix that helps me search through Gentoo’s repository much faster than the standard tools will allow. Here’s an example of the alias I use:
alias update=’emerge –sync ; eix-update ; emerge -avuD world’
localhost techgage # update
>>> Starting rsync with rsync://22.214.171.124/gentoo-portage…
These are the packages that would be merged, in order:
Because the output is truly long, I shortened it up just to show how it would run. Depending on your distribution, the process is of course going to be different, but the point here is that you really can combine even multiple tedious commands into one that’s unbelievably simple. In this particular example, I turned 47 characters into 6. Not bad… not bad at all.
To help wrap this up, I’ll drum out a couple of more examples without delving too deep into what each one of them accomplish, since for the most part, they’re self-explanatory:
alias restart-net=’/etc/init.d/net.eth0 restart’
localhost techgage # /etc/init.d/net.eth1 restart
* Unmounting network filesystems …
* Stopping sshd …
* Stopping eth1
* Bringing down eth1
* Stopping dhcpcd on eth1 …
* Shutting down eth1 …
* Starting eth1
alias ip=’lynx -dump http://www.whatismyip.com/automation/n09230945.asp’
[email protected] ~ $ ip
alias xlog=’tail /var/log/Xorg.0.log’
[email protected] ~ $ xlog
(**) Option “xkb_layout” “us”
(II) config/hal: Adding input device Power Button
(**) Power Button: always reports core events
(**) Power Button: Device: “/dev/input/event0”
(II) Power Button: Found keys
(II) Power Button: Configuring as keyboard
(II) XINPUT: Adding extended input device “Power Button” (type: KEYBOARD)
(**) Option “xkb_rules” “evdev”
(**) Option “xkb_model” “evdev”
(**) Option “xkb_layout” “us”
alias keywords=’nano -w /etc/portage/package.keywords’
GNU nano 2.2.4 File: /etc/portage/package.keywords
alias trash=’du -sh ~/.local/share/Trash/’
[email protected] ~ $ trash
As I’m sure is evident by this point, there really is no limit to the number of ways the alias command can save you both time and effort, and overall, it can help make the command-line be an even cooler place to hang out. After looking at all of the examples here, I hope you’ve come up with some ideas on how you can use alias to your own advantage, and if so, be sure to post about them in our forums! We’re always up for gaining some new ideas ourselves!
Before I wrap up, though, an article about aliases wouldn’t be complete without a mention of “ln”, a command that shares similar goals as alias. This command is used to create symbolic links (symlinks) between either files or folders which can both act as a typical shortcut or the linking of one file to another. The latter example is important when dealing with system files on occasion, especially if some application you’re using is looking for a file in the incorrect location.
For me personally, I mostly use them to create shortcuts to various folders. One example is under my home folder. Because that folder tends to be quicker to access than any other, I symlinked my documents folder (which is located on another drive) to a faux folder in my home folder. So, when I open up my home folder and click on the documents folder, it still looks like I’m truly in my home folder, but in reality I’m fishing around on the alternate hard drive. Here’s a command-line example:
ln -sfn /mnt/storage/documents/ documents
[email protected] ~ $ ls -l documents*
lrwxrwxrwx 1 techgage techgage 23 Oct 1 2009 documents -> /mnt/storage/documents/
See? It’s just that simple! Combined with both alias and symlinks, you’ll not only feel like the master of your own domain, but you’ll be a lot more productive at the same time!
Have a comment you wish to make on this article? Recommendations? Criticism? Feel free to head over to our related thread and put your words to our virtual paper! There is no requirement to register in order to respond to these threads, but it sure doesn’t hurt!
Copyright © 2005-2019 Techgage Networks Inc. - All Rights Reserved.