Using Aliases in the Linux Shell

Creating aliases to use in the command line is a simple way to increase productivity and shows the simplicity and power of using the shell. Here’s how to do it.

This tutorial is written for Centos 5.2 and bash, but should work on most linux distributions.

What is an alias?

An alias is just another term for something. It is the same for the command line. You can specify any name you like for a command so for example if you do not like using ‘ls’ to show you everything in a folder you can change it to ‘showme’ or ‘ilovedancingnaked’. Whatever you like in fact.

Why bother?

The simple answer is that it will save you time. Take this example. Let’s say that each time I list the contents of a folder I use ‘ls -lhF’. It is my preference that I see the long listing format, in human readable format and that the output classifies the listing. If this is a persistent preference I can use an alias to make it permanent.

How to do it

To create an alias you will need to login to your home directory. Run ‘ls -a’ and you should see a hidden file .bash_profile. This is the file we need to add the alias to. So let’s edit that file:

vi bash_profile 

Then we add the alias – you can add this anywhere in the file.

alias ls='ls -lhF' 


for example for hsphere server

alias qmailstop=’/etc/init.d/qmail stop’
alias qmailstart=’/etc/init.d/qmail start’
alias qmailrestart=’/etc/init.d/qmail restart’
alias httpdreload=’/etc/init.d/httpd reload’
alias httpdstop=’/etc/init.d/httpd stop’
alias httpdstart=’/etc/init.d/httpd start’
alias message=’tail -f /var/log/messages’
alias maillog=’tail -f /var/log/maillog’
alias apachelog=’tail -f /hsphere/shared/apache/logs/access_log’
alias apacheerror=’tail -f /hsphere/shared/apache/logs/error_log’

 



Quit and save by hitting ‘:wq’. Then we need to reload the profile

source .bash_profile 

All done! Now whenever you use the ‘ls’ command the .bash_profile file will override the default options and return the options that you have specified in the alias, so you’ll see a human readable directory listing without having to add the flags

Advertisements
Leave a comment

Leave your opinion

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: