Friday, February 18, 2011

Linux Lessons (another lesson) Aliases

Aliases is a strong utility in linux arsenal, which benefits the using of your linux machine. With aliases you can create simple "key phrases" which execute complicated and/or long commands. You can have a file with your aliases, or just create instant aliases in the terminal with a short life (until you close your terminal).

Let's see first how to create an instant alias, just for your current terminal session.
All actions were made in Ubuntu with GNOME as the desktop environment.

Open your terminal:
We are going to create an alias for a simple command: the command that change directory to our video files!!!
The path of the videos is: ~/Videos
The command to go there is: cd ~/Videos

We can just create an alias for going to that directory, with giving only the word videos. Like this:
alias videos="cd ~/Videos"
and press enter.
Now try it:
videos
press enter

Let's see another command which is longer and we would use it most of the times if it wasn't so big to write. The command that locks your screen which is (in gnome) :

gnome-screensaver-command -l


We've created this alias:
alias lock="gnome-screensaver-command -l"

Now try lock. (After that put your password and return here)

These are good but they have a disadvantage: They live short. What if you want to use them e.g for ever?
For this purpose you can create a file in your home directory with the appropriate name. The name of that file is found in another file in ~/.bashrc. Remember that you can view the file using less ~/.bashrc. The important part who says the name of the file is at the end of the .bashrc.
Here it is in our case:



It's name is .bash_aliases.
Let's create that file. type:
touch .bash_aliases
Now your file is created but it's empty.
Let's put the aliases we looked at(for every new line press enter:
cat >>~/.bash_aliases
or use
nano >>~/.bash_aliases
(if you
alias videos="cd ~/Videos"

alias lock="gnome-screensaver-command -l"
^d
(The previous was ctrl+d which means that you won't send any more data)

Let us now give you some ideas for other aliases:
your favourite website:
alias vaslabs='firefox "http://www.vaslabs.blogspot.com"'
the shutdown and restart commands:
alias shutdown="sudo shutdown -P now"
alias restart="sudo shutdown -r now"

What other aliases can you think of? Send us if you want your aliases on vaslabsco@gmail.com

No comments:

Post a Comment