FUNDAMENTALS A Complete Guide for Beginners
The alias command in Linux is a widely used command that is focused on productivity and functionality. This command allows the creation of a shortcut in the command line. This is very useful for developers who frequently need to type similar complex commands and it saves a lot of precious time.
A. Description
The alias command in Linux is used as a shortcut that takes a string that can execute a command and replaces it with another string of text. This command can replace a large complex command with a relatively simple string and reuse that command many times. Professionals use this command quite a lot to increase their functionality.
B. Syntax
There is no man page for the alias command. However, you do have a help option for the alias command.
alias [option] [name]='[value]'
Note: You must keep the command/value inside a quotation. You can use either a single quotation or a double quotation.
C. Options
There are not many options available for the alias command. As there is no man page for the alias command, you can try out the –help option to learn more about it.
alias --help
Useful Options
- -p (Lists all available aliases in the command line)
- –help (Shows the help page of the alias command)
Note: To remove an alias you need to use another command called the unalias command which you will know about in another section.
Practical Examples of the “alias” Command in Linux
The alias command is a frequently used command for professionals. It does not have a variety of uses, but it does have importance for the functionality it provides.
Example 1: Creating a Temporary Alias Using the “alias” Command
You can create a temporary alias quite easily. But one thing you need to remember, a temporary alias only remains until the terminal is closed. The general syntax for the alias command is as follows.
alias ALIAS="Command"
You can follow the steps below to create an alias for the ls -l command.
Steps to Follow >
➊ Open the Terminal in the Home directory.
➋ Type the following command in the command line.
alias ll="ls -l"
➌ Now, press the ENTER button.
Output >
As you can see, a new alias has been created and it runs exactly as it should.
Example 2: Listing All Available Aliases
You may want to display all the available/set aliases. To do that you can just type alias or use the option -p option which lists all the aliases in the system. You can follow the steps below.
Steps to Follow >
➊ Open the Terminal in the Home directory.
➋ Type the following commands in the command line.
alias
Or,
alias -p
➌ Now, press the ENTER button after both commands
Output >
You will find all the set aliases in the system.
- The “echo” Command in Linux [7 Practical Examples]
- The “unalias” Command in Linux [3 Practical Examples]
- The “exit” Command in Linux [5 Practical Examples]
Example 3: Creating a Permanent Alias in Linux
Sometimes, you may want to create a permanent alias that will remain in the command line permanently. To do that, you need to edit file names .bashrc which is a hidden file you won’t find in the GUI normally. To access the file you can use the nano text editor. You can follow the steps below the try this out.
Steps to Follow >
➊ Open the Terminal in the Home directory.
➋ Type the following command in the command line to open the .bashrc file.
nano ~/.bashrc
➌ Scroll down in the alias section of the file and type the custom aliases you want to make permanent.
#Custom aliases
alias ll='ls -l'
alias dl='cd Desktop/LinuxSimply'
alias di='rm -i'
❹ Press CTRL+S to SAVE the file and CTRL+X to EXIT the file to the command line.
❺ Now press type the bash command to load the file and the alias command to check the available aliases. You can also close the Terminal and open a new one then type alias to check.
bash
alias
❻ Press the ENTER button.
Note: You can type the new custom aliases anywhere you want, but I recommend you to type it in the alias section as it will keep the file tidy and later on find it easy to customize.
Output >
As you can see that the permanent aliases are set and when you use the alias command, the new aliases are set.
Conclusion
In this article, you learned about the use of the alias command in Linux and how they can increase productivity and functionality. It is a useful command but you don’t really need to use it always. But I recommend you learn to use aliases as professionals always use them for their daily use and once they are used to using aliases efficiently, they can’t go back anymore. To be a professional, you need to practice some of these aliases in your day-to-day usage of Linux.
Related Articles
- The “bash” Command in Linux [5 Practical Examples]
- The “clear” Command in Linux [3 Practical Examples]
- The “declare” Command in Linux [7 Practical Examples]
- The “export” Command in Linux [8 Practical Examples]
- The “source” Command in Linux [3 Practical Examples]
- The “hash” Command in Linux [6 Practical Examples]
- The “read” Command in Linux [8 Practical Examples]
- The “set” Command in Linux [6 Practical Examples]
- The “unset” Command in Linux [2 Practical Examples]
- The “eval” Command in Linux [4 Practical Examples]