The ALIAS Command Saves Repetitive Typing

Unix-based operating systems, including Linux and Mac OS X, have an alias command that saves time.  If you find yourself typing in the same commands repeatedly, consider setting up an alias for them.  For example, say you do a lot of work in a folder called “~/current_projects/annual_report”, such that you type this command a lot:

cd ~/current_projects/annual_report

you could make an alias for it like this:

alias cdar='cd ~/current_projects/annual_report'

and from then on, just type cdar, instead of typing the whole thing.  Note that the alias’ definition is enclosed in single-quotes (apostrophes).

Aliases come in especially hand for launching java-based programs.  For example, the H2 database comes with an easy to use, browser-based tool for exploring the contents of the database.  The hardest part of using it is remembering the command to launch it:

java -jar ~/java_libs/h2-1.0.74.jar

So, setting up an alias like this:

alias h2="java -jar ~/java_libs/h2-1.0.74.jar"

means that launching it is simplified to merely typing in h2.

Note that you can string multiple commands together, with semicolons, in the same alias definition.  So, for example, if every time you launch the h2 tool, you want to first make sure that the current directory is your home folder, then do this:

alias h2="cd ~;java -jar ~/java_libs/h2-1.0.74.jar"

Tip: Type alias by itself to find out what your current aliases are.

Tip: Tab-completion works with aliases.  Type the first couple of letters of your alias name and then hit tab.

Tip: Aliases can refer to other aliases:

Warning: Alias definitions only stick around for as long as you are logged in.  So, to make an alias act as if it is permanent, you have to re-define it every time you log in.  Fortunately, there’s an easy way to do that automatically, which I’ll cover in my next post.

Tip: Look for a future post in which I’ll describe how to examine the Linux command history.  This is an easy way to explore the commands you frequently enter, thus giving you ideas about which ones ought to be turned into aliases.

Related articles:

Post a Comment

Your email is never published nor shared. Required fields are marked *

© 2006-2007 Maxim Software Corp.  All rights reserved.