Thursday, October 11, 2007

How to create command alias to save time on Ubuntu

Do you have a long task (or line of command) that you run on your terminal window very frequently? If you do and want to save some time, this how to will for sure help you.

Let's say you are always connecting to a remote computer via ssh, with some different parameters than the defaults. Lets say you usually run the code:

$ ssh -v [user]@[ip or hostname] -p 2222

To save some time, you can also configure a "shortcut", or a alias, that when you type on the terminal window will call for the command above. Something like:

$ connecthome

The process is very simple. All you have to do is edit a file on your /home/user folder called ".bashrc". You can even add more options and edit some commands. For example, let's say you want Ubuntu to confirm that you want to delete a file every time you use "rm" on a terminal window (I did because I'm a noob, we make a lot of mistakes!). All you have to do is add the line:

alias rm='rm -i'

The "-i" sign means "interactive". Ubuntu will ask you to confirm that you really want the delete the file. Type "man rm" on a terminal window for more info.

So here's what you should add to your .bashrc to achieve the modifications above:

alias rm='rm -i'
alias connecthome='$ ssh -v [user]@[ip or hostname] -p 2222'

First make sure you backup your .bashrc and them edit the main one:

$ sudo cp .bashrc .bashrc.bak
$ sudo nano .bashrc

Press Ctrl+x to save, and "y" to confirm overwrite. The change will only take effect after you login again. But you can always check on the another tty. Type Ctrl+Alt+F1, login with you username and password and try it out. To go back to X just type Ctrl+Alt+F7.

Here's what a copy of my .bashrc looks like:

# Basics
alias cp='cp -v -i'
alias rm='rm -i'
alias mv='mv -i'
alias utar='tar -xvf'
alias utarz='tar -xzvf'
alias tarz='tar -czvf'

# Software Management
alias aptl='sudo apt-get update && sudo apt-get install'
alias update='sudo apt-get update'
alias upgrade='sudo apt-get upgrade'
alias inst='sudo apt-get install'
alias apts='sudo apt-cache search'


Vic.

1 comment:

Anonymous said...

yeah.. bookmarked post