Linux Crash Course – Bash Aliases

Aliases in Bash enable you to essentially create your own commands, or even just simplify existing ones. In this video, I’ll explain what aliases are, and I’ll show you some of my personal favorites. At the end, I’ll show you how to make your aliases persist between sessions.

YouTube player

First of all, let’s look at a simple example of creating an alias.

We have the alias command for that, and the syntax will look something like this:

alias mycmd="ls -lh"

In that example, I created an alias named mycmd, abbreviated, and when executed, it will instead run ls -lh.

Of course, this particular alias isn’t all that useful, because you can probably type ls -lh in the same amount of time you can type mycmd. But the takeaway here, is that you can call your alias whatever you want, so you can create your own commands or shorten longer commands.

Here’s another example:

alias l="ls -lh"

In that example, we’ve created an alias that’s down to just a single character

l

Now that command is going to save us some time, we can type a single character and it will execute the ls -lh command.

To see a list of aliases, you can run alias with no arguments:

alias

To remove an alias, you can use the unalias command:

unalias l

Now, if we run the alias command, we won’t see the l alias that we’ve created anymore, we’ve removed it.

How about we check out some useful examples of aliases?

First, let’s check out the df -h command:

df -h

As we’ve gone over in other videos, the df command will show us the amount of storage space we have available on mounted filesystems.

But there’s also a lot of output that I don’t care about. Let’s create an alias.

alias df='df -h -x squashfs -x tmpfs -x devtmpfs'

Now, when I type df, the output will be simpler:

df

I didn’t have to add the -h option for human readable output, and I also don’t see filesystems I don’t care about. The -x option for df is the exclude option, so when we created the alias, we set it up so that it will exclude several types of filesystems.

The mount command is useful, not just for mounting filesystems, but also for showing what’s already mounted. The output is a bit all over the place though. Let’s change that.

alias lsmount='mount |column -t'

Let’s see how the mount command output differs now:

lsmount

Looks much cleaner to me! I didn’t want to override the normal mount command, so I gave it a different name. lsmount isn’t the shortest name, but you can shorten it if you’d like.

Are you curious what the public IP is of your Internet connection?

alias extip='curl icanhazip.com'

Now we can find that out anytime we want, by running extip:

extip

Now, let’s create some aliases that are going to help us manage packages easier.

alias install='sudo apt install'

This particular alias is specific to Debian and Ubuntu. But you can adjust that command to fit your distro if you’re running something else.

Now, we can install a package by typing the word install, followed by a package name:

install tmux

Or, we can shorten that down even more:

alias i='sudo apt install'

So now we can install a package by simply typing i, followed by a package name.

i tmux

Similarly, we can simplify update commands as well:

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

This command implies that you’re running Debian or Ubuntu, but if you’re running another distro, you can replace the contents of the alias with whatever command you use on your distro to update packages.

For the next alias, we will need Python installed. Your Python binary might be python or python3, so you may have to adjust the next command depending on your distro.

alias speedtest='curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python -'

If you don’t have Python, you’ll need to install it for that alias to work.

speedtest

With the speedtest alias, I can now get a speedtest anytime I run the command. Keep in mind though, the speedtest command might not give you an accurate test of your bandwidth speed if your computer is behind a switch, router, or firewall that has slower ports. But as long as you’re connected to relatively decent hardware, you should get a decent speed test.

Finally, I have a few more aliases to show you that I think will be very helpful.

alias mem5='ps auxf | sort -nr -k 4 | head -5'
alias cpu5='ps auxf | sort -nr -k 3 | head -5'

I just created two aliases, mem5, and cpu5. Both will show the top 5 resource hungry processes for both memory and cpu respectively.

Before I close the video, I definitely want to discuss a very important topic around aliases – and that’s how to make them persist. If I was to close my terminal window, I’ll lose all the aliases that I’ve created. I definitely won’t want to recreate those each time. The answer to this riddle is to set them up in your .bashrc config file.

nano ~/.bashrc

All you have to do is simply copy and paste the aliases into a section within this file, and then every terminal you open will automatically have the aliases you’ve created available.

Now, when I open up a new terminal, I’ll have all the aliases set up that I’ve created.

Check out the Shop!

Support Linux Learning and get yourself some cool Linux swag!


Support LearnLinuxTV and receive 5% off an LPI exam voucher!