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:
root@root [~]# u
-bash: u: command not found
First, the command ‘u‘ isn’t under the Linux. We can set the alias here;
root@root [~]# alias 'u=uptime'
root@root [~]# 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
root@root[~]# cd /root/
root@root[~]# 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:
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:
root@root[~]# 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:
root@jishnu [~]# u
20:28:20 up 18 days, 22:21, 1 user, load average: 0.00, 0.00, 0.00
root@jishnu [~]# unalias u
root@jishnu [~]# u
-bash: u: command not found
To unset permanently, remove the entry from ~/.bashrc file.
That’s it. 🙂
Related posts:
top, ps, find, ftp commands, tar, rpm, Rsync,