The Alias Command in Linux

The Alias Command in Linux

In this article, I go over the concept of aliases in Linux.

Overview

We tend to write more commands on our terminal which might not seem to be a problem, but when we need to write out long commands time and time again, it becomes more of a hassle and a bit frustrating, because we try to memorize these long commands.

What if there was a way to shorten these long commands or assign them to a much shorter and easy-to-remember value? Well, that is what the alias command is for.

What are Aliases?

Aliases are shortcuts that point to a particular command, it represents a command in the Linux shell with a user-defined entry.

Why create new Aliases?

Users may want to create aliases for a variety of reasons.

Some users may require aliases to aid in the execution of long commands; others may require aliases to rename existing commands to names they prefer; and still, others may simply wish to experiment.

How to create new Aliases

When creating aliases we can create two types here, the first is a Temporary Alias, the second is a Permanent Alias.

Temporary Alias

Temporary aliases can only be used in the shell where they were created. If you end the session of that shell, all the aliases you made in that shell will also be gone.

We will see how to create temporary aliases first.

To create a temporary alias in Linux, the syntax is as follows:

$ alias your_command_name="command"

Here is an alias for the command for listing the top 5 largest files in a directory on your system, sorted in descending order.

$ alias ls-bigdir="du -a /{dir}/ | sort -n -r | head -n 20"

Here is an alias for installing packages on Linux.

$ alias get="sudo apt install"

image-of-get-alias

See what happens when we try to access our get alias in a different terminal window

second-image-of-get-alias This is the only downside to creating temporary aliases.

Permanent Alias

Compared to the temporary alias, when you create a permanent alias, you can access that alias within any terminal session.

When creating a permanent alias, you need to add the alias to your .bashrc file.

To do this, open your .bashrc file in your favorite text editor, and add your alias like so:

nano ~/.bashrc

Add your alias in the file (mainly at the bottom of the file) like this:

bashrc-image

We have added the get alias to our .bashrc file. Save and exit the file.

You have successfully created a permanent alias.

Now, we can access our alias in any terminal window.

first-terminal-window

Screenshot from 2022-06-06 10-31-32.png

Conclusion

You can create more aliases if you want, just note that if you want to have a permanent alias, add it to your .bashrc file.

I hope you found this topic interesting, and I hope you've learned something new about Linux today. If you found this helpful, leave a like and share with others. Cheers!!