Thursday, May 8, 2008

/dev/null

In Unix-like operating systems, /dev/null or the null device is a special file that discards all data written to it (but reports that the write operation succeeded), and provides no data to any process that reads from it (it returns EOF). In Unix programmer jargon, it may also be called the bit bucket or black hole.

The null device is typically used for disposing of unwanted output streams of a process, or as a convenient empty file for input streams. This is usually done by redirection.

Technically /dev/null is defined as a null device. Anything written to it is lost forever. If you read the wiki description of /dev/null, you will get to know lots of jargons/metaphors written/referred by UNIX techies a lot. In this article, we will concentrate on the importance of /dev/null, why it is there, and how to use it.

Importance

When things written to it are lost forever, then why do we need it? This is a valid questions, but we need /dev/null/ to lose information. That's correct, to explain let's take an example:

Example 1:

Suppose, you want to list all files of directory /var/tmp/ having word "foo" in its content. To achieve the task, we will write something as:

]$ grep -l foo /var/tmp/*
/var/tmp/storagedata
/var/tmp/storagedata.0
grep: /var/tmp/td.log: Permission denied

So we got two files having word "foo"; but we also an annoying error message which was part of STDERR. We were not interested in any error message, and we wanted to see only those files which I am permitted to read. So, how do I get rid of this error message? Luckily newer version of grep provides a silent option "-s", using which we can get rid of this message. But what if I am working on a traditional system, having traditional grep? What if the system command is not having the silent option? The solution is given below:

Capturing STDOUT only

  1. Using newer grep:
       ]$ grep -ls foo /var/tmp/*
    /var/tmp/storagedata
    /var/tmp/storagedata.0
  2. Using traditional grep:
       ]$ grep -l foo /var/tmp/* 2>/dev/null
    /var/tmp/storagedata
    /var/tmp/storagedata.0
  3. In general using any system command :
       ]$ cmd 2>/dev/null

Most of you should have captured the importance of /dev/null by now. In simple words, by using "2>/dev/null", we are asking the shell to redirect the STDERR to /dev/null. This is very useful when we execute system command through a program and expect to get only the STDOUT.

There may also be a case, when we are only interested to see the error message (STDERR), and are not at all interested in STDOUT. To get STDERR, and to discard the STDOUT we will redirect the STDERR to STDOUT and will redirect the STDOUT to /dev/null; as given below:

Capturing STDERR only

]$ cmd 2>&1 1>/dev/null

You may be thinking, that if I am redirecting STDERR to STDOUT which in-turn is redirected to /dev/null, so there should not be any output. But that is not the case, and you get the error messages.


Thursday, April 24, 2008

find the Nth max sal in employee table.

Here is the query to find the Nth max salary of an employee:

SELECT distinct(salary) from employee order by salary desc limit(n-1,1);

like for 3rd highest salary query will be:

SELECT distict (salary) from employee order by salary desc limit(2,1);

Tuesday, April 1, 2008

Linux Directory Structure

A brief introduction to the Linux Directory Structure

Overview

One of the most noticable differences between Linux and Windows is the directory structure. Not only is the format different, but the logic of where to find things is different.

The Format

In Windows, you use this format to access a directory:

Code:
C:\Folder1\subfolder\file.txt

In Linux, this is the basic format:

Code:
/Folder1/subfolder/file.txtYou'll notice that the slashes are forward slashes in Linux versus backslashes in Windows. Also, there is no drive name (C:, D:, etc.) in Linux. At boot, the 'root partition' is mounted at /. All files, folders, devices and drives are mounted under /. Though it is not apparent from this example, it is important to note that files and folders in Linux are case sensitive. /Folder1/subfolder/file.txt is not the same as /folder1/subfolder/file.txt.

The Main Directories

These are the basic directories that you (should) have after installing any Linux distribution:

Quote:
/bin/
/dev/
/etc/
/home/
/lib/
/mnt/
/proc/
/root/
/sbin/
/tmp/
/usr/
/var/

/bin/

This is where all your programs that are accessible to all users will be stored once installed.

/dev/

This is a virtual directory where your devices are 'stored.' Devfs allows Linux to list devices (hard drives, input devices, modems, sound cards, etc.) as 'files.'

/etc/

This is where you'll find all your global settings. Daemons such as ssh, telnet, and smtp/pop3 mail servers find their configuration files here. Also in /etc/ is the system's password file, group lists, user skeletons, and cron jobs.

/home/

This is the default directory where non-root users' homes are created. When you add a user, the default home directory is created as /home/username. You can change this default in the proper file in /etc/.

/lib/

This is where shared libraries (perl, python, C, etc.) are stored. Also in /lib/ are your kernel modules.

/mnt/

This is the default location for mounting cdroms, floppy disk drives, USB memory sticks, etc. You can mount anything anywhere, but by default there is a /mnt/floppy (if you have a floppy drive) and /mnt/cdrom.

/proc/

This virtual folder contains information about your system. You can view processor statistics/specifications, PCI bus information, ISA bus information, and pretty much anything else you want to know about the hardware on your system.

/root/

This is the default home directory for the user root.

/sbin/

This is where system programs are installed. These include fdisk, tools to make partitions, certain network tools, and other things that normal users shouldn't have a need for.

/tmp/

This is the default location to place files for temporary use. When you install a program, it uses /tmp/ to put files during installation that won't be needed once the program is installed.

/usr/

This contains various programs, non-daemon program settings and program resources.

/var/

This is where your log files, system mail messages and database of installed programs are स्तोरेड
 
Custom Search