How to set ‘alias’ for a command in Unix/Linux – alias/unalias command usages with examples

It’s a simple but powerful concept under the Linux. This setup will reduce your time on command prompt by simplifying your commands with your own shortcuts or short forms. For example, you can simply find the server UPTIME by accessing a single word like ‘U’, if you have set an alias for the command “UPTIME” under the Linux.

How to set ‘alias’ for a command?
You can use the command ‘alias’ for setting Alias to a Linux command. Here I am explaining the usages,

Usages:

alias 'u=uptime'
Or
alias u='uptime'

Here I have selected the command “uptime” for illustrating the ‘alias’.
Example:

[email protected] [~]# u
-bash: u: command not found

First, the command ‘u‘ isn’t under the Linux. We can set the alias here;

[email protected] [~]# alias 'u=uptime'
[email protected] [~]# u
 19:45:39 up 18 days, 21:38,  1 user,  load average: 0.00, 0.00, 0.00

This method is a temporary way to set alias. That means, after you have done a server reboot or login again after logout from the server, the alias will vanish from the server. You can set the alias permanently by editing the ‘bashrc’ file. Here is the way to set Alias permanently.

How to set an alias permanently?
Simply by editing the .bashrc file under the root location and add the alias entry in that file. First change the directory to its default location(/root) then open the .bashrc file by using your favorite file editor.

vi ~/.bashrc
Or
[email protected][~]# cd /root/
[email protected][~]# vi .bashrc
---
Add the alias entry: 
---

The changes will effect after a server reboot. For a quick result you can sourcing that file.

Example:

source ~/.bashrc

Example:

alias

How/Command to list all aliases set on the server?
You can see the already set aliases by executing the following commands.

alias
Or
alias -p
Or 
compgen -a

Example:

[email protected][~]# alias -p
alias attrib='chmod'
alias chdir='cd'
alias copy='cp'
alias cp='cp -i'
alias d='dir'
alias del='rm'
alias deltree='rm -r'
alias dir='/bin/ls $LS_OPTIONS --format=vertical'
...

How to unset the alias which you have done previously?
Yes, we must have an idea about this. How to unset alias? simply you can use the command ‘unalias’ for doing the same.

Command:

unalias $aliasname

Example:

[email protected] [~]# u
 20:28:20 up 18 days, 22:21,  1 user,  load average: 0.00, 0.00, 0.00
[email protected] [~]# unalias u
[email protected] [~]# u
-bash: u: command not found

To unset permanently, remove the entry from ~/.bashrc file.

That’s it. 🙂

 

Related posts:
topps, find, ftp commandstarrpmRsync,

Arunlal Ashok

Linux Server Administrator. I'm dealing with Linux servers since 2012. I started this blog to share and discuss my ideas with the world. Check My Profile!! in uPwork (oDesk) and let me know if you need any assistance. Thanks!!

You may also like...

Leave a Reply

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