Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Using UNIX aliases to save keystrokes UNIX
If you're a complete beginner to UNIX (as I still consider myself), you can use something called aliases to save yourself a lot of typing at the command line.

In the Mac OS, an alias is simply a pointer to another file. In the UNIX world, an alias is similar in concept, except that it's a command that points at another command. There are a few pre-defined aliases in the tcsh shell (the program that runs when you open a terminal). You can see these by typing
at the command line. One of the more useful pre-defined aliases is ll which replaces ls -lag for complete file listings.

Although the pre-defined aliases are useful, the real power is in creating your own aliases for your often-used commands. If you're new to UNIX and you'd like to learn about aliases and how to use them, read the rest of this article.

You can easily define your own aliases in UNIX. Here's an example. When I connect to my home machine from work, I use the secure shell (SSH) built into OS X. However, to connect to the machine, I have to enter the following command (phony IP address listed, of course!):
ssh -l robg -p 7022
This is obviously a bit of a pain. However, I can easily create an alias to do all the hard stuff:
alias hssh 'ssh -l robg -p 7022'
Within that terminal session, whenever I type hssh, I'll connect to my home machine. To remove the alias, you use (logically enough) the unalias command:
unalias hssh
However, if I close the window and open a new terminal session, the alias will be gone. Aliases entered at the command line are effective for that session only.

To make the aliases stick, you need to save them in a file that the tcsh shell reads each time you open a terminal session. This file is called .tcshrc, and it lives at the top level of your home directory (/Users/username). To create it, simply open it in your favorite editor:
vi /Users/robg/.tcshrc
Enter any aliases you'd like to use in the form of
alias aliasname 'command to be aliased'
You only need to use the quotes if the command to be aliased contains spaces; separate aliases with a carriage return. So my sample .tcshrc file might look something like
alias hssh 'ssh -l robg -p 7022'   ** My home machine **
alias mssh 'ssh -l macosxhints' ** The MacOSXHints webserver **
alias wcd 'cd /Library/WebServer/Documents' ** My local webserver folder **
Obviously, don't enter the comments noted following the '**' in the examples above.

When you're done, save the file, and the next time you open a terminal window, you'll have your aliases ready for use. This is a very very brief overview of the use of aliases (and the .tcshrc file). Both can be used in much more advanced ways, and for many other purposes. I'm sure some of the more UNIX-savvy users out there can probably list a bunch of other cool things to do with aliases and the .tcshrc file, but this primer should get you started.
  • Currently 2.67 / 5
  You rated: 5 / 5 (6 votes cast)

Using UNIX aliases to save keystrokes | 4 comments | Create New Account
Click here to return to the 'Using UNIX aliases to save keystrokes' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Changing global aliases
Authored by: tapella on Feb 02, '01 08:28:46PM

If you want to change the "built-in" aliases or add aliases that every user gets automatically, you edit a different file:

This assumes that you're using tcsh as your shell, of course. tcsh is the default Terminal shell in OS X, so you can just edit the above file with the default install.

PS I'm partial to ls -alF myself :-)

[ Reply to This | # ]
Authored by: robh on Feb 05, '01 09:19:26AM

Another handy trick with aliases is to use !* to embed arguments inside an alias, e.g.

alias zlines 'zcat !* | wc -l'

So that the command "zlines myfile.gz" is expanded into "zcat myfile.gz | wc -l", i.e. a tool to count lines in a gz-compressed file.

Also useful for users of aliases is 'which'. This command will show you either where the command is located or what it is an alias for, e.g.

which zcat

which zlines
zlines: aliased to zcat !* | wc -l

If you find aliases useful for creating shorter more memorable commands, another tip I'd give is to set up environment variables in your .tcshrc (or .cshrc) file, e.g.

setenv LOGS /Library/WebServer/Logs/
setenv WEBLOG /Library/WebServer/Logs/apache_access_log
setenv ERRLOG /Library/WebServer/Logs/apache_error_log

Then you don't need to remember where your webserver access log is (handy if you work with multiple machines where the location can vary), you can then do thing like this..

cd $LOGS
tail -f $WEBLOG

Note that "tcsh" will autocomplete environment variables when you hit the TAB key, e.g. if you typed "wc $WE" then hit TAB, tcsh will fill in the rest to make it "wc $WEBLOG".

[ Reply to This | # ]
Authored by: iMike on Mar 08, '01 02:57:59AM
You're better off not creating aliases in: '/usr/share/init/tcsh/tcsh.defaults' '/Users/robg/.tcshrc' Unix is a shared computing environment. Changing '/usr/share/init/tcsh/tcsh.defaults' affects all users--this is generally not good. Changing '/Users/robg/.tcshrc' only affects robg, however, it is less clean than creating a separate aliases.mine file. The more appropriate place to create user aliases is in the following place: '/Users/yourloginnamehere/Library/init/tcsh/aliases.mine' Be sure to replace "yourloginnamehere" with your login name.

[ Reply to This | # ]
the very first alias
Authored by: theRegent on Jul 07, '02 11:24:34PM

If you're going to be editing your aliases often (or, as in my case, if you just don't want to have to remember how to get to it in order to edit it later when you discover other uses for it) you can create an alias to the aliases list.

By naming it something mind-boggling obvious you should avoid any conflicts with more commonly used aliases or any element of your shell or running programs, such as:

alias editthataliaslistingthatishiddendeepinthesystem ' pico ~/Library/init/etc....

This is obviously hyperbole, but my point is that this alias doesn't have to be short and doesn't have to be easy to remember instantly... it needs to be very, very easily remembered. Ideally, you won't need it very often.

[ Reply to This | # ]