An Introduction to Linux Aliases

Between Me and The Terminal

I’ve used Linux for a long time, although there was a long period during the 2010s when I just gave up on Linux for a while. However, the time I was away from Linux made me more open to learning how it worked once I eventually came back.

A decade ago, I only knew how to write a handful of Linux commands, but extremely basic things. I’m talking:

sudo apt-get update && sudo apt-get upgrade

And that’s no typo. I mean apt-get and not apt; that’s how long ago this was!

However, if I wanted to install most things, I didn’t even know how to search apt from the terminal. I would just throw my hands up and use Synaptic to find packages.

These days, I’m running vanilla Arch, so searching for a package is only a pacman -Ss command away, but what if there was a way to make this even easier? Why type that whole thing in several keystrokes when I could do it in just a couple? Why type neofetch the entire time when I could likely shorten that as well? If we have keyboard shortcuts available at our fingertips to make common tasks faster in a desktop environment or window manager, why can’t we have a similar optimization in the terminal?

Fortunately, the solution is quite simple! We can set up an alias to make running command commands much faster.

How Does An Alias Work?

It’s even simpler than you would expect. Let’s say I frequently type pacman -Ss to search for something in the repos (you could replace this with anything that suits your package manager). What if I could shorten pacman -Ss instead of typing that whole string of characters each time?

With an alias, I can set everything up so that I can substitute search for that query. This way:

pacman -Ss firefox

Could easily be replaced by:

search firefox

Seems cool, right?

You could easily extend it beyond just searching. For instance, Ubuntu users who want to update the system can set an alias so they can simply type update instead of sudo apt update && sudo apt upgrade.

How Do I Get Started With Setting Up An Alias?

You’re in luck, because it’s quite easy. It should take you no more than a few minutes, really.

For the sake of example, I will assume readers are running an Ubuntu-based system since it’s a rather popular distribution. As a result, I’ll teach everyone how to set up an alias so that update runs sudo apt update && sudo apt upgrade. I don’t really run any Ubuntu systems on a regular basis, but I think this is an excellent example of a command that could really benefit from being shortened.

STEP 1: Start up a terminal of your choosing (I’m a fan of Alacritty myself, but the default one your system comes with should be sufficient).

STEP 2: Open up your .bashrc or .zshrc with a text editor of your choosing.

If you’re on something like Ubuntu, then chances are you’re going to edit your .bashrc, as Bash is the default shell on the vast majority of Linux systems. A minority of installations will require editing .zshrc or something else entirely. However, 90% of the time, your system will come with Bash by default, so go with .bashrc instead.

As for your text editor, many people find Nano easy to use, although I’m personally a fan of Vim.

nano .bashrc

vim .bashrc

If you’re understandably still not too comfortable with a command line text editor, you could always forego the terminal for now and use a graphical program like Gedit or Kate to open it up. The fun part here is that you could even launch the graphical program from your terminal!

gedit .bashrc

If you happen to be new to Linux or this is a fresh install, there’s a slight chance your distro may not even have a .bashrc file out of the box. I don’t feel like spinning up a virtual machine to confirm which distros do or don’t, but if you find a blank file when opening up .bashrc, then there’s a chance it didn’t exist yet. This nothing to be afraid of; once you save changes in the file and reload, they’ll take effect (more on how to do that in a later step).

STEP 3: Find a random blank line in your .bashrc where you can type. Don’t overthink where exactly in the document to type, although if you need a suggestion, the end of your file is usually an acceptable place. Most of the time, everything is fine as long as the line is blank.

Once you decide on where to type, enter the following:

alias update='sudo apt update && sudo apt upgrade'

STEP 4: Save the changes to your file. If you happen to be saving in a graphical editor and your .bashrc somehow didn’t exist, then make sure to save it to your Home folder.

STEP 5: Close the editor and return to the terminal.

STEP 6: Reload your changes by either closing the terminal window and opening a fresh one OR, alternatively, typing bash and hitting enter. Both of these will reload your changes from your .bashrc file.

STEP 7: Give it a shot! Now whenever you type update, your terminal will know to treat it the same way as if you typed sudo apt update && sudo apt upgrade.

update

By typing it, you should now see your Ubuntu system checking if any updates are available.

What If It’s Not Working?

There are a few simple things you can try for troubleshooting.

  • Be absolutely sure you saved your .bashrc file in your Home directory, not your Documents, Downloads, or anywhere else. Your terminal will typically load your home directory by default on startup, so be sure you create, open, and save the file in this directory. If you want to be dead certain you’re in your Home directory, simply type cd and hit enter.
  • Can’t see the file at all in your Home directory? Even if you know it’s somehow there? In case you didn’t know, any file starting with a period in a Linux system (or anything based off Unix) is treated as a hidden file. If you’re trying to browse your file manager and open it up the old-fashioned way (whatever makes you feel comfortable), be sure to look in the file manager’s “View” or “Settings” menus for an option to show hidden files. Several more files may become visible in your Home folder, and that should include your .bashrc file.
  • Is the new alias just not working when you type it? Double check the .bashrc file again to be sure you entered everything exactly as it’s written above. Typos can be annoying. In addition, don’t leave the single quotes open or it won’t work.
  • Everything look fine in the file and still no luck? Don’t underestimate the importance of Step 6; starting a fresh terminal window or just typing bash to refresh your changes is a must.
  • Are you sure you’re running Bash by default? Bash is default on most systems, although I’ve read conflicting reports that certain distros like Manjaro (only some flavors, possibly?) and Garuda will come with Zsh as default. To check for sure, type echo $SHELL into your terminal and hit enter. If you see /usr/bin/zsh appear, then that means you’re running Zsh and need to edit a .zshrc file instead. If the result says something like /bin/bash instead, then you’re using Bash and .bashrc is what you want.
  • Want instructions on how to do this with the likes of Linux Mint or Pop_OS? I didn’t single these excellent distros out because they are Ubuntu-based. As a result, the above tutorial for creating an update alias will work exactly the same way.

I Got It Working! What Else Can I Do With Aliases?

I have a couple suggestions listed below.

Run neofetch with just nf, a fraction of the keystrokes:

alias nf='neofetch'

Run apt search as just search to look up packages to install on an Ubuntu-based system:

alias search='apt search'

Run sudo apt install as just install on said Ubuntu-based systems:

alias install='sudo apt install'

Both of the above aliases are set up so you can just type search firefox or install firefox in order to search for or install Firefox respectively.

I’ll close out with a favorite I picked up from DistroTube on YouTube: To make the ls command a little more useful, install the exa package beforehand or this following alias won’t work.

sudo apt install exa

OR if you followed the above example and want to give it a try:

install exa

Once you have exa installed, add this alias to your .bashrc:

alias ls='exa -al --color=always --group-directories-first'

Reload your configuration and, whenever you type ls to look through a directory within your terminal, you’ll see a much more detailed and organized color-coded list of files.

I Don’t Like Some of These Changes. How Do I Get Rid Of Them?

If you don’t like an alias you set up for whatever reason, you can easily go back into your .bashrc, erase the line containing the alias you don’t want, save, and reload your Bash shell again. You could also change or further optimize some of the above commands should you desire.

Did you find this tutorial helpful? What are some other tasks you would like to streamline through aliases? What other aliases do you use on a regular basis? Were you interested in seeing how to use the update, install, or search commands with alternative package managers like pacman, dnf, or zypper?

One response to “An Introduction to Linux Aliases”

Leave a reply to Dumping Spotify and Paid Music Streaming For My Offline Music Collection – Sudo Science Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.