Alias in Linux

FREE Online Courses: Dive into Knowledge for Free. Learn More!

In this article, you will learn all there is about the alias command in Linux-based operating systems. We will be going through what the alias command is, why it is used, the syntax of the alias command, and also different commands for managing aliases like creating, listing, and deleting.

What is the alias command?

An alias is a command-line tool in Linux based operating system. It replaces or creates an alias to a string that invokes a command. In simple words, the alias command is used to abbreviate a command in the terminal.

Alias is available on many other operating systems other than Linux like AmigaDOS, Windows PowerShell, ReactOS, KolibiriOS, EFI shell, and 4DOS/4NT. In Windows and MS-DOS, the functionality of the alias command is carried out by the DOSKey command.

Initially, the alias command was introduced only in the C shell, which also meant that it would function in descendant shells like bash and tcsh. It was only later that the alias command was imported to the Korn Shell and Bourne Again shell.

To sum up, the alias command is used to replace or create abbreviations for long commands, improving efficiency. It can also replace commands with additional options, hence making them easier to use.

Why use the alias command?

Let us say that there is a really long command or another really helpful command that has a long set of options, it is obviously hard to remember the spelling or the set of options. The alias command comes in handy in exactly these kinds of situations.

You can abbreviate a really long option set or command to just 2 characters! Also, if you think about it, a typical Linux user would reuse the same command again and again, leading to copying, pasting, and typing again and again which is such a waste of time.

Hence by using the alias command, you can save yourself so much time by creating aliases for most of the commands you use frequently. The best part is that, you can create custom aliases, hence you can create names you are comfortable with and the ones you can remember.

Syntax of Linux alias command

Before we look at the different alias management commands, let us start from ground zero by looking at the syntax. The syntax of the alias command is extremely simple:

alias <option> <abbreviation>=’<command>’

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Let us take a closer look at the fields in the alias syntax:

1. Options

This field allows you to enter different options available with the alias command.

2. Abbreviation

This field is where you enter your custom alias name or abbreviation.

3. Command

This field takes the name of the actual command you want to create the abbreviation or alias for.

Now that we laid down the fundamentals, let us take a look at some of the alias management commands.

Listing aliases in Linux

To list all the aliases that are currently defined in your system, simply type ‘alias’ in the terminal

listing aliases using the alias command

Let us take an example of alias to get a better understanding of they work. One of the alias in the about out put is ‘ll’, which is an abbreviation or an alias fro the command ‘ls -alF’. You can see that running the commands “ll” and ‘ls -alF’ is producing the same output.

example of alias command

ll is alias for ls alF

Creating a temporary alias in Linux

To create an alias for a specific command, we use the syntax we discussed above. Personally, for writing articles and putting screenshots of outputs for you to understand better, I found myself using the clear command very often to keep the terminal neat snd cumbersome-free.

So let us create a shortcut for the command ‘clear’ by using the command “alias c=’clear’. This command creates an alias c for the clear command. Now, I can simply type ‘c’ to clear the screen instead of typing C-L-E-A-R!

creating temporary alias

If you want to check if your alias has been successfully created or not, you can type the command “alias” and see if your alias is present. You must note that the alias list is alphabetically ordered.

You can also create aliases for opening text files and running shell programs. For example, to open the text file lamorghini.txt, you can create an alias by using the command “alias cars=’<filepath>’”

example of creating a temporary alias

Now running the command “cars” will produce the same result as running the command

cat /home/gopikiran/Desktop/cars/lamorghini.txt

output of the temporariliy created alias

Creating a permanent Alias in Linux

If you want to keep your aliases permanently (in between sessions) in Linux, you can open an editor and write it in there. However, this command depends on the shell you are using:

1. Bash shell: ~/.bashrc

2. Zsh shell: ~/.zshrc

3. Fish shell: ~./config/fish/config.fish

You must type the command according to the shell you are using, since I am using the bash shell, I am going to open the nano editor by using the command “sudo nano ~/.bashrc”

command to open configuration file of alias

creating alias permanently in configuration file

In the text editor, you can write your aliases and save them. These aliases are permanently there in your system until you open the text editor again and remove it from there. Since we are on the topic of removing aliases, might as well learn how to do that from the terminal.

Removing an alias in Linux

We have seen that we can remove a permanent alias by opening the text editor and removing the alias you wrote previously. We can also do the same from the terminal by using the command unalias using the following syntax:

unalias <alias>

removing an alias

If you want to confirm that the alias has been removed, you can check it by running the “alias” command.

Creating Linux alias with multiple arguments

You can even create an alias for a command that has multiple arguments. For example, say you want to see the contents of multiple directories. You can do so by using the ls command as follows: ls <directory1> <directory2>

For example, the command “ls cars DataFlair” will show the contents in the directories cars and DataFlair.

creating alias with multiple arguments

You can alias this command by using the same syntax we discussed above. To check if it has been successfully created, you can run the “alias” command:

creating a temporary alias example

 

Now you can run the alias name you have created and it will give you the same output as the command you aliased.

testing the example of alias with multiple arguments

Using this, you can create an alias for any command. In fact, using the syntaxes we discussed above, you can list, delete and create aliases.

Summary

In this article, you have learned what the alias command is, why it is used, the syntax of the alias command, and also different commands for managing aliases like creating, listing, and deleting.

Your opinion matters
Please write your valuable feedback about DataFlair on Google

follow dataflair on YouTube

Leave a Reply

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