Creating Command Aliases in Linux (Command Shortcuts)

Last Updated on: August 22, 2020

As time goes on, you’ll need to be super productive with the way you use commands in Linux, and one example is utilizing alias.

Using alias allows you to create an additional equivalent name for a command, for example, you can simplify commands to one word or few letters, say you want to simplify sudo apt install to only ‘install’, you use the following command:

					

alias install="sudo apt install"

Entering the above command will provide no output, but behind the scene, a new command ‘install’ is now an alias(equals) to ‘sudo apt install’

Verify the alias by running the ‘alias’ command:

1. All Aliases

You should see the alias you just created, you’ll also see additional aliases in the output, If you are using Ubuntu, it has some alias created by default, for example, l is the same as ls.

Now when you want to install any packages, you can simply do the following:

install nginx

Super simple right! you don’t have to type ‘sudo apt install nginx’, if you are way too lazy, you can simplify it further:

					

alias i="sudo apt install"

Now by adding ‘i <package name>, you can install anything pretty fast with less typing.

One thing to note before we go further is that, when you exit your terminal window, your aliases are wiped out, ouch!. Fortunately, you can retain them by editing the .bashrc file, this file is present in your home directory
and is read every time you start a new Terminal session.

We can add an alias in there, but it is recommended we add it into a new file: ~/.bash_aliases, this makes it easier to maintain.

Once you are done, reload your bashrc file using:

Now, that you understand alias, let’s see some cool example:

Clear Screen by Typing ‘c’

Jump Into Apache Log Folder

					

alias apl="cd /var/log/apache2"

We can be creative with the above command, why not jump into the folder, and also list the files in there? See the below command:

					

alias apl="cd /var/log/apache2 && ls"

This would replace the first one because I am using the same name ‘apl’

Jump Into Nginx Log Folder and List All the Files in the Folder

					

alias ngl="cd /var/log/nginx && ls"

View the Top Ten Cpu Consuming Processes:

					

alias cpu10='ps auxf | sort -nr -k 3 | head -10'

View the Top Ten Ram Consuming Processes:

					

alias mem10='ps auxf | sort -nr -k 4 | head -10'

Jump To Users Home Directory

Removing alias

To remove alias, you use the ‘unalias’ keyword, so, to remove alias whos name is home, I’ll do:

The best way to come up with a aliases, is to think of commands you use on a regular basis, and simplify it, take a look at your bash history for commands you use.

Gracias

Comment policy: Respectful and beneficial comments are welcome with full open hands. However, all comments are manually moderated and those that doesn't relate with what the passage is saying or offensive comments would be deleted. Thanks for understanding!

Leave a Reply

Your email address will not be published. Required fields are marked *