Tips Linux Explorers   All Things Linux Forum   Great Linux Links   Hometown   Email 


To make your commandline experiences even more fun then they are already, here is a tip every lazy Linux user will love.
For long and complicated commands we can set an alias, that way an often repeated command can be brought back to one or two characters.

There are only a few rules you have to take in account: 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.

O.K. lets first set up some example files and directories:

NOTE: you will see the full prompt "bruno:~$" in these commands because in this case it is needed to show you how it works.

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

( Will make a directory /extra in /home/bruno/downloads/programs, we use the -p argument in order to make all the underlying directories as well if they do not already exist. )

bruno:~$ vi ~/downloads/programs/extra/index

( Make a textfile called ¨index¨ where we will write a simple text: )

i ( to put vi in insert mode ) and type the line:

¨This is an index of downloaded programs¨

Esc + ZZ ( to save the file )

Good, the example files are created, now the real fun starts, we set up the aliases first:

bruno:~$ alias c='cd '

( Note the space after cd !!! 
This is because you can only use 2 aliases one after the other if you have that space ! )

bruno:~$ alias dp='~/downloads/programs/extra'

Now we will use the aliases:

bruno:~$ c dp

( so these are the alias for the command; cd /home/bruno/downloads/programs/extra )

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


bruno:~/downloads/programs/extra$ ls

( lets see what is in that directory )

QUOTE (Text @ Screen)

We make another alias, note the space after cat !

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

bruno:~/downloads/programs/extra$ s index

( short for ¨cat index¨ )

QUOTE (Text @ Screen)
This is an index of downloaded programs

( and that was the line we typed in the file )

We use the c alias again:

bruno:~/downloads/programs/extra$ c ~

( will put you back where we started )

bruno:~$ rm -rf ~/downloads/programs

( will remove the remains of our little test, but leaves the directory downloads as it was before the test )

bruno:~$ unalias c dp s

( will undo the aliases we made here above )

NOTE: these aliases will only be there for as long as you do not log-out ! If you want to use them on a permanent basis you can write them in in /home/bruno/.bashrc ( clever if you have to use the same long commands every day  ).

IMPORTANT: You can do "alias -p" to see what aliases are already set on your system, so you won't use ( overwrite ) one that is already in use !!


-- Jun 8 2003 ( Revised Dec 10 2005 ) --

Tips Linux Explorers   All Things Linux Forum   Great Linux Links   Hometown   Email