How to create, list, delete alias or aliases in Redhat or Centos Linux ?

Today we will look at an interesting topic of alias or aliases in Redhat or Centos Linux.

1. List current aliases.
alias and “alias -p” commands are synonym to each other.

[root@nglinux ~]# alias 
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@nglinux ~]# 

OR,
[root@nglinux ~]# alias -p
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@nglinux ~]# 

 

2. Creating new alias.

[root@nglinux ~]# alias msg="echo 'Welcome to NGELinux.com'"

[root@nglinux ~]# msg 
Welcome to NGELinux.com

[root@nglinux ~]# alias 
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias msg='echo '\''Welcome to NGELinux.com'\'''
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@nglinux ~]# 

 

3. Removing alias: Use of unalias command.

[root@nglinux ~]# unalias msg

[root@nglinux ~]# alias 
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

[root@nglinux ~]# msg
-bash: msg: command not found
[root@nglinux ~]# 

 

4. Making alias permanent.
There is no option with alias command to make it permanent.
Every time we reboot out system, the aliases gets removed.

To make it permanent, we need to add the alias command in our bashrc file (/etc/bashrc for system-wide seetings, and .bashrc for user-wide settings).

Let us take an example:

[root@nglinux ~]# cat /etc/profile | grep -i alias
# Functions and aliases go in /etc/bashrc
[root@nglinux ~]# cat /etc/bashrc | grep -i alias
# System wide functions and aliases

[root@nglinux ~]# cat .bash_profile  | grep -i alias
# Get the aliases and functions

[root@nglinux ~]# cat .bashrc | grep -i alias
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

 

5. Tip: How to run a command’s unaliased version.

[root@nglinux ~]# cd /
[root@nglinux /]# ls
bin  boot  cgroup  dev  etc  home  lib  lost+found  media  mnt  newpart  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var
[root@nglinux /]# \ls
bin  boot  cgroup  dev	etc  home  lib	lost+found  media  mnt	newpart  opt  proc  root  sbin	selinux  srv  sys  tmp	usr  var
[root@nglinux /]# alias ls
alias ls='ls --color=auto'
[root@nglinux /]#

To understand this, lets have a look at below screenshot.
We can see the color was there earlier, since the alias has “–color” parameter with it.
And when we ran its unaliased version we can see the list without color.

aliases ngelinux.com

 

Leave a Reply

Your email address will not be published.