Linux: Aliases
-
In Linux it is common to use the alias function in order to modify system behaviour in handy ways. Most Linux distros today ship with a stock list of alias modifications to make their systems easier, safer or just unique. The alias command is used to both display existing aliases and to create or modify our own aliases.
An alias in Linux is just what it sounds like, an alternative way to refer to something. Most commonly an alias might be used to reduce a common, complex command into something very short and simple or to make a common typo correct.
For example, it is not uncommon for a full time Windows Admin who only needs Linux sometimes to alias dir to ls so that they can accidentally type dir and get what they expected.
If we just use the alias command with no options, it will display the aliases currently set on our system. Let's see what are the default aliases on a stock CentOS 7 installation:
# alias alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
As you can see, there are seven default aliases included in CentOS 7. Most of these are pretty basic, just modifying the default egrep, fgrep, grep and ls commands to have full colour by default. Nothing too exciting.
We can make our own aliases for whatever we want. Typically this is not recommended, but it is a facility that is available and should be understood by system administrators as it would be easy to hide or override functionality using aliases and if they are not understood it can lead to confusion.
Why We Do Not Recommend Aliases for Most System Administration Functions This same reasoning will come up many times as a good general practice for SAs. By and large we do not want to customize our systems. System customizations are important for end uses who need to be efficient at repetitive tasks and daily duties. End users can often take time to set up their environments, tweak settings and such to make their environments work as they want them to work. As System Admins, we rarely have this luxury. What is most important is that we cannot guarantee that we will have these tweaks and settings when things matter most - when something is broken. Whether it is being used to having a "safety" feature to catch us from mistakes or a quick reference to a file we use, it is generally better that we learn to do things the standard way because when it is most important, that may be the only option that we have.
We will make and remove a simple alias here to learn out it works. Our alias will take us directly to the /tmp directory.
# alias go2t="cd /tmp"
Not very practical, but it shows how an alias works. Now we can test it.
# go2t # pwd /tmp
That was easy. But we do not want to keep that hanging around. We can remove an alias by setting it to null like so...
# unalias go2t
And the alias is gone. Run the alias command again and you will see that our go2t alias is not in the list, it has been erased.
-
@scottalanmiller How do make a permanent alias? For example I want to shorten clear to cls.
-
@alex.olynyk said:
@scottalanmiller How do make a permanent alias? For example I want to shorten clear to cls.
# alias cls="clear"
Then add it into your .bashrc file for just you or into the /etc/profile for everyone on a system.
-
Or your .profile if you want it across all of your shells.
-
This post is deleted! -
@aaron said:
@scottalanmiller I am enjoying these write-ups and would like to share them with some of my junior staff. Is there an index page of sorts ?
There are ways to show them as an index. One is the official "table of contents" which presents the entire series "in order":
http://mangolassi.it/topic/7825/sam-learning-linux-system-administration
And then there is the "SAM Linux Administration" tag which shows all of the articles tagged as part of the series which will show them more in "order they were written":
-
My plan is to get a small "banner" that goes across the top of each and points back to the main page to make navigation much easier and clearer. Just need to get a banner made, though
-
@alex.olynyk said:
@scottalanmiller How do make a permanent alias? For example I want to shorten clear to cls.
You can just use ctrl+l then you don't even need the alias.
-
In order for me to properly remove the sample "go2t" alias I had to use
unalias go2t alias alias ls='ls --color=auto'
Prior to unalias I had
alias alias go2t='' alias ls='ls --color=auto'
-
@DustinB3403 Thanks. OP has been fixed.
-
Instead of just using aliases, set your home directory in your PATH before /usr/bin/local and create replacement programs to irritate anyone using your user account... that's what I do for fun on my personal machine.
-
@tonyshowoff said in Linux: Aliases:
Instead of just using aliases, set your home directory in your PATH before /usr/bin/local and create replacement programs to irritate anyone using your user account... that's what I do for fun on my personal machine.
If it's just your local machine..... why not do that system wide? (Apparently I'm feeling especially evil tonight.)
-
You know just because I hate having to type "clear" every time... every linux system I have is getting the "cls" alias!
-
@DustinB3403 said in Linux: Aliases:
You know just because I hate having to type "clear" every time... every linux system I have is getting the "cls" alias!
You can just do ctrl+l
-
@johnhooks said in Linux: Aliases:
@DustinB3403 said in Linux: Aliases:
You know just because I hate having to type "clear" every time... every linux system I have is getting the "cls" alias!
You can just do ctrl+l
And have to remember yet another keyboard shortcut?!
-
@DustinB3403 said in Linux: Aliases:
@johnhooks said in Linux: Aliases:
@DustinB3403 said in Linux: Aliases:
You know just because I hate having to type "clear" every time... every linux system I have is getting the "cls" alias!
You can just do ctrl+l
And have to remember yet another keyboard shortcut?!
SOME of us had nothing BUT keyboard shortcuts to work with at one point Seriously, real, physical RS-232 terminals with 64 keys were no fun! I can't even find pictures of the one I was stuck with, it was numbers, letters, and a ctrl key.
-
@DustinB3403 said in Linux: Aliases:
@johnhooks said in Linux: Aliases:
@DustinB3403 said in Linux: Aliases:
You know just because I hate having to type "clear" every time... every linux system I have is getting the "cls" alias!
You can just do ctrl+l
And have to remember yet another keyboard shortcut?!
ctrl + l (L) is pretty universal for clear screen in the POSIX-compliant world, it's really about learning standard short cuts, like ctrl + k is delete line, ctrl + c is SIGINT, hell the latter even works in MSDOS and PowerShell.