Using Aliases in Linux to Make Life Easier

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.

Examples of Useful Aliases; Final Thoughts

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’ssh -i /root/.ssh/rsync-key [email protected]
localhost techgage #
Last login: Thu Apr 1 12:00:00 2010 from
[[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://…

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’
[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

kde-base/kdebase-cursors ~amd64
kde-base/kdebase-data ~amd64
kde-base/kdebase-desktoptheme ~amd64
kde-base/kdebase-kioslaves ~amd64
kde-base/kdebase-menu ~amd64

alias trash=’du -sh ~/.local/share/Trash/’
[email protected] ~ $ trash
6.9G /home/techgage/.local/share/Trash

Final Thoughts

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!

Discuss this article in our forums!

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!

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 googleplus icon instagram icon