18. Command-Line Aliases
For those used to graphical point-and-click interfaces, using command lines may require more than a little dusting off of those skills. Thankfully Linux has a way to make your command-line experiences more efficient. You can shorten long and complicated commands by assigning them an Alias to type instead. That way an often repeated command can be brought back to one or two characters.

The format of the alias command is as follows:

alias [name=['command']]

"Name" is the alias you want type and "command" is the command line the alias should replace.

There are only a few things to remember. The alias comes always directly after the prompt and if you want to use more of them in one line you'll have to add a space as you set the alias.


IMPORTANT: The tips in this document require the use of command-line commands. For more information about how to read and execute Linux command-line prompts and commands, please check the Linux Clues Linux Cheat Sheet, especially Linux Prompt Basics and Linux Command-Line Nomenclature.


Note: For many tips we start by changing user to root, but that's not necessary for this one. All the commands are given as user. (No need to do "su".) For our example commands, the username is shown as bruno.

So let's set up some sample files and directories. First, create a directory /extra in /home/bruno/downloads/programs. Use the -p argument to make all the underlying directories, if they do not already exist. Type:

$ mkdir -p ~/downloads/programs/extra/

Next, make a text file called "index" where we will write a simple text.

$ vi ~/downloads/programs/extra/index

Then to put vi in insert mode. Type:


Add text to the file as follows:

This is an index of downloaded programs.

Save the file:

Esc + ZZ

The sample files are created, so now the real fun starts. Let's create some aliases for the command: cd /home/bruno/downloads/programs/extra. Type:

$ alias c='cd ' (Note the space after cd!)
$ alias dp='~/downloads/programs/extra'

Then type:

$ c dp

And the proof that c dp actually worked here is the new prompt:


Let's see what is in that directory. Type:

~/downloads/programs/extra$ ls

The result is:


Let's make another alias, note the space after cat. (Cat displays the contents of a file.)

~/downloads/programs/extra$ alias s='cat '

Then type:

~/downloads/programs/extra$ s index

You'll see the text you typed into the file named index:

This is an index of downloaded programs.

Then to get back where you started, type:

~/downloads/programs/extra$ c ~

To remove the test files and restore the directory downloads as it was before the test, type:

$ rm -rf ~/downloads/programs

And to undo all the aliases set by the user:

$ unalias c dp s

These aliases will only be there for as long as you are logged in. They aren't saved and will disappear when you log out. To use them on a permanent basis, write them in /home/bruno/.bashrc. It's a good idea to do this if you use the long commands over and over again.

You can also use "alias -p" to see what aliases are already set on your system, so you won't overwrite one that is already in use.

That's what you need to know about aliases under Linux!

Errata About Hard Links
The last installment of Linux Explorer, Hard Links and Symbolic Links, contained an error that's been fixed on the website edition of Scot's Newsletter. The sentence: "Hardlinks are cool, and there's nothing like them under Windows" is technically incorrect. Thanks to SFNL reader Darryl Sequeira, and others, for pointing out that under NTFS in Windows XP, there's a utility called FSUTIL that can create hard links that work similarly to the ones in Linux. The MSDN article Hard Links explains how.