• Home
  • Shell
    • Emacs
    • Perl
    • screen
    • sed
  • Ubuntu
    • VNC
  • Web Development
    • Javascript
    • Joomla
    • MySQL
    • osTicket
  • Windows
    • Gimp

Linux> Aliases

Jun08
2011
Written by Scott Rowley

Aliases can be a great way to more easily remember an oddball command or to shorten a long command. Aliases are stored in your .profile (or .bash_profile) or within a include file referenced from .profile.

A couple of quick examples before showing how to set them up:

Original Command:

scottro@sudobash.net ~> cd /home/scottro/bin/tools/sandbox
scottro@sudobash.net /home/scottro/bin/tools/sandbox>

Aliased Command:

scottro@sudobash.net ~> sbox
scottro@sudobash.net /home/scottro/bin/tools/sandbox>

Original command:

scottro@sudobash.net ~> cp -r /home/scottro/website/ /var/website/backups

Aliased Command:

scottro@sudobash.net ~> backup

Much faster to type in that alias isn’t it? This is especially useful when you have a command that you use all the time. First we’ll take a look at a simple alias.

Basic Aliases

alias tucows="whois -h whois.tucows.com"

Now instead of typing whois -h whois.tucows.com example.com every time I want to do a lookup referencing tucows I can simply type the following

tucows sudobash.net

This is clearly easier to remember as well as faster to type.

Conditional Aliases
Those aliases are great but what if you are running in an environment where you are hopping from server to server and your alias breaks? For instance, on one server you may be using grep to include color (grep –color) but on other servers this option is not present and every time you grep you will receive an error message complaining about how –color isn’t a valid option.
Note: This is only an issue if you are using a mounted home directory that will use the same .profile across multiple servers. If you don’t do that then you’ll have to setup your aliases individually on each server (a pain for large environments).

grep: illegal option -- color
Usage: grep -hblcnsviw pattern file . . .

What we can do to fix this then is put in checks to see if we are on a particular server by any given attribute. Examples follow.

Now lets say we’ve got two different servers here one being Solaris and one being Ubuntu.

Example alias:

grep="/opt/csw/bin/ggrep --color=always"

Solaris test command (using the grep alias)

scottro@sudobash.net ~> grep test *
test.txt:test

This test worked fine because ggrep is installed and the color option present. Lets try the same thing now on our Ubuntu server.

Ubuntu test command (using the same alias)

scottro@sudobash.net ~> grep test *
bash: /opt/csw/bin/ggrep: No such file or directory


This demonstrates that our alias would work on one server but not on another (In this case it doesn’t matter — Ubuntu can have color grep by default, we don’t have to tell it to use ggrep).

So how do we fix this so that we can use grep in both locations with both of them working? On this particular example we’ll check if ggrep is actually installed (and where we expect it to be) and if it is then use the alias.

if [ -f /opt/csw/bin/ggrep ]
then
alias grep="/opt/csw/bin/ggrep --color=always"
fi

Ok, so we’ve seen the test examples. Where on earth does all this stuff go? As previously mentioned just paste and edit your code in the .profile file in your home directory OR you can have a separate file named whatever you like (I recommend .bash_aliases) and then reference that file in your .profile (this makes for a less messy .profile)

# Load Aliases
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
else
echo "No alias file found"
fi

Reloading your profile (and aliases), make sure you are in your home directory and:

. .profile

Useful Aliases

alias epoch='perl -e "print time"'
Posted in BASH, Ubuntu - Tagged alias, basic, conditional, epoch, ggrep, grep, if, include, linux, Perl, print, profile, reload, solaris, time, ubuntu, unix, whois
« Joomla> Repair c99madshell hacked site
» osTickets> Reports v5.0

Corrections? Questions? Comments?

Find an error?
Everything work out great for you?
Have some feedback?
Like to see something added to the article?

PLEASE leave us a comment after the article and let us know how we are doing, or if something needs corrected, improved or clarified.

Thank you!
- The Management

Advertisement

Sudo Bash
By Geeks - For Geeks

Back to Top