How to Create and Use Alias Command in Linux

Linux users often need to use one command over and over again. Typing or copying the same command, again and again, reduces your productivity and distracts you from what you are actually doing.

 

You can save yourself some time by creating aliases for your most used commands. Aliases are like custom shortcuts used to represent a command (or set of commands) executed with or without custom options. Chances are you are already using aliases on your Linux system.

 

List Currently Defined Aliases in Linux

You can see a list of defined aliases on your profile by simply executing alias command.

$ alias

 

How to Create Aliases in Linux

 

Creating aliases is a relatively easy and quick process. You can create two types of aliases – temporary ones and permanent. We will review both types.

 

Creating Temporary Aliases

 

What you need to do is type the word alias then use the name you wish to use to execute a command followed by "=" sign and quote the command you wish to alias.

 

The syntax is as follows:

$ alias shortName="your custom command here"

 

Here is an actual example:

$ alias wr=”cd /var/www/html”

 

You can then use "wr" shortcut to go to the webroot directory. The problem with that alias is that it will only be available for your current terminal session.

 

Creating Permanent Aliases

 

To keep aliases between sessions, you can save them in your user’s shell configuration profile file. This can be:

Bash – ~/.bashrc
ZSH – ~/.zshrc
Fish – ~/.config/fish/config.fish

 

The syntax you should use is practically the same as creating a temporary alias. The only difference comes from the fact that you will be saving it in a file this time. So for example, in bash, you can open .bashrc file with your favourite editor like this:

$ vim ~/.bashrc

 

Find a place in the file, where you want to keep the aliases. For example, you can add them in the end of the file. For organizations purposes you can leave a comment before your aliases something like this:

#My custom aliases
alias home=”ssh -i ~/.ssh/mykep.pem rootadminz@192.168.0.100”
alias ll="ls -alF"

 

Save the file. The file will be automatically loaded in your next session. If you want to use the newly defined alias in the current session, issue the following command:

$ source ~/.bashrc

 

To remove an alias added via the command line can be unaliased using unalias command.

$ unalias alias_name
$ unalias -a [remove all alias]

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)