Wednesday 31 August 2005

Vim - The powerful simple editor

You select any Linux/Unix OS distribution; from the spartan linux which fits on a floppy to the industry heavy weights like redhat and suse; you are guaranteed to find the vi editor. So it is really worth ones time to learn how to use this powerful but simple editor. Vim is the modern version of the vi editor. Learning to use vim (vi) contains an initial learning curve. But the power it gives the user to accomplish complex text manipulation with just a few keystrokes makes the trouble taken to learn worth it.

Here I will explain a few commonly used commands in Vim. Vim (vi) is an editor with modes of operation. There are three modes of operation in vim. They are as follows :
Read more »

Who owns which port ?

Linux contains a lot of command line tools which make the job of a network administrator easier. One of them is netstat. Netstat is a multi-purpose network informantion tool. Using netstat, you can find which port is used by which process or user by using the following command:

# netstat -an | more
There is a utility called fuser which also tells you which user and process owns a port. For example, if you want to find who owns port 631, you execute the following command:

$ fuser -v -n tcp 631
Note : Ports below 1024 are reserved for common services , and only root can use them. Standard port numbers can be found in /etc/services file. The rest of the over 65K ports can be used by normal users or processes.

Convert MS Word Files to Other formats using Abiword

Abiword is a word processor which can be used as an alternative to popular commercial counterparts. It comes installed by default on most Linux distributions.

Did you know that you can convert from one file type to another in the command line using Abiword ? This is how it is done. Read more »

Tuesday 30 August 2005

Bash Shell Shortcuts

Bash, which is the default shell in Linux contains a whole lot of key bindings which makes it really easy to use . The most commonly used shortcuts are listed below :

____________CTRL Key Bound_____________
Ctrl + a - Jump to the start of the line
Ctrl + b - Move back a char
Ctrl + c - Terminate the command
Ctrl + d - Delete from under the cursor
Ctrl + e - Jump to the end of the line
Ctrl + f - Move forward a char
Ctrl + k - Delete to EOL
Ctrl + l - Clear the screen
Ctrl + r - Search the history backwards
Ctrl + R - Search the history backwards with multi occurrence
Ctrl + u - Delete backward from cursor
Ctrl + xx - Move between EOL and current cursor position
Ctrl + x @ - Show possible hostname completions
Ctrl + z - Suspend/ Stop the command
____________ALT Key Bound___________
Alt + < - Move to the first line in the history
Alt + > - Move to the last line in the history
Alt + ? - Show current completion list
Alt + * - Insert all possible completions
Alt + / - Attempt to complete filename
Alt + . - Yank last argument to previous command
Alt + b - Move backward
Alt + c - Capitalize the word
Alt + d - Delete word
Alt + f - Move forward
Alt + l - Make word lowercase
Alt + n - Search the history forwards non-incremental
Alt + p - Search the history backwards non-incremental
Alt + r - Recall command
Alt + t - Move words around
Alt + u - Make word uppercase
Alt + back-space - Delete backward from cursor

----------------More Special Keybindings-------------------

Here "2T" means Press TAB twice

$ 2T - All available commands(common)
$ (string)2T - All available commands starting with (string)
$ /2T - Entire directory structure including Hidden one
$ 2T - Only Sub Dirs inside including Hidden one
$ *2T - Only Sub Dirs inside without Hidden one
$ ~2T - All Present Users on system from "/etc/passwd"
$ $2T - All Sys variables
$ @2T - Entries from "/etc/hosts"
$ =2T - Output like ls or dir

Tuesday 23 August 2005

Installing and Configuring Java in Linux

Java is a revolutionary language which was invented by James Gosling at Sun Microsystems. The most endearing thing about Java is its platform independence - which means you can literally compile it on one platform and then run it on any other platform be it Sparc, Intel, Motorola or PowerPC architecture running any of the OSes (Windows, OSX, Linux...). Recently I had the opportunity to work in Java/J2EE technology. And naturally I wanted to do programming in Linux instead of windows (basically because I am more productive and comfortable working in Linux than windows). Here I will detail the steps I took to install and configure Sun's JDK 1.5.0 on Linux.
  • I Download the latest JDK (Java Development Kit - latest is ver 1.5.0) for Linux from Sun's java site. It was a zip file which, when I unpacked into my home directory, created a directory named 'jdk1.5.0'.
  • Then I logged in as 'root' and moved the just created 'jdk1.5.0' directory into the /opt directory.
  • Now again logging in as normal user, I appended the following lines to my .bashrc file.
#FILE .bashrc
...
JAVA_HOME=/opt/jdk1.5.0
export JAVA_HOME

export PATH=$PATH:/opt/jdk1.5.0/bin

CLASSPATH=/opt/jdk1.5.0/lib/tools.jar: \ /opt/jdk1.5.0/lib/dt.jar
export CLASSPATH
...

As you can see in the above listing, I have set the JAVA_HOME variable to point to my Java installation directory. This is needed because some application servers like JBoss will be checking if Java is installed on your system using JAVA_HOME variable. And I have also modified the PATH variable to point to the Java bin directory. I have set a third variable CLASSPATH which points to the libraries needed to compile Java programs. This is needed because when you compile your Java programs in the command line, the compiler needs to know where the libraries are located. This it achieves by reading the CLASSPATH variable. Don't forget to export JAVA_HOME, PATH and CLASSPATH. Once you have edited your .bashrc file, open a terminal and execute the command to make the bash shell to re-read the contents of the .bashrc file as follows:
$ source ~/.bashrc
Now you can compile and execute Java programs in Linux just like you do in windows.

MySQL integration in your Java Programs
Java has excellent support and integration with all major databases through the JDBC API. Since MySQL - a time tested robust database - comes bundled by default with most Linux distributions, any Java programmer on the Linux platform need not look at alternative databases. But to program in Java using JDBC to connect to MySQL database, you have to download and install the MySQL-Java driver. Below I list the steps needed to configure Java to work with MySQL.
  • First go to the MySQL website and download the MySQL ConnectorJ driver for Java file (Size: 6.6 MB). When I downloaded, the latest version was 3.1.10. (mysql-connector-java-3.1.10.tgz ), which might have changed by now. This is a gzipped tape archive. So unpack it into a directory of your choice as follows:
    $ tar -xvzf mysql-connector-java-3.1.10.tgz
    This will unpack it into a new directory by name mysql-connector-java-3.1.10.
  • Now login as root and move the whole directory into the /opt directory.
  • Next logout as root and login as normal user and modify CLASSPATH in your .bashrc file as follows:
# FILE .bashrc
...
CLASSPATH=/opt/jdk1.5.0/lib/tools.jar:/opt/jdk1.5.0/lib/dt.jar \\ :/opt/mysql-connector-java-3.1.10/ \ mysql-connector-java-3.1.10-bin.jar
export CLASSPATH
...

That is it. Now you can write JDBC code in Java to connect to MySQL just like you do it in windows or any other platform. Below I have written the section where you need to enter MySQL specific code :

# Program Snippet
...
try{
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
String url = "jdbc:mysql://localhost:3306/mydatabasename";
Connection con = DriverManager.getConnection(url,"username","password");
// The rest of the code is common for all databases.
}
...