File system Structure I
Here we will see how Unix/Linux File system is organized and what commands can be used to search for files and programs.

1.File System Layout

The first noticeable difference between Linux/Unix and a DOS or Windows system is the filesystem. For starters, we do not use drive letters to denote different partitions. Under Linux, there is one main directory. You can relate this to the C: drive under DOS. Each partition on your system is mounted to a directory on the main directory. It's kind of like an ever-expanding hard disk.

We call the main directory the root directory, and it's denoted with a single slash (/). This concept may seem strange, but it actually makes life easy for you when you want to add more space. For example, let's say you run out of space on the drive that has /home on it. Most people install Linux and make one big root drive. Well, since a partition can be mounted to any directory, you can simply go to the store and pick up a new hard drive and mount it to /home. You've now “grafted” on some more space to your system. And all without having to move many things around.

  The files are organized into a tree structure with a root named by the character '/'. The first few levels of the tree look like this:

                              /
|
-------------------------------
/ | | | \
etc bin usr tmp dev
| | |
---- ---- ----
/ \ / \ / \
ls . csh bin lib ucb. lib

Below, you will find descriptions of the major top level directories under Linux.

/bin

Essential user programs are stored here. These represent the bare minimum set of programs required for a user to use the system. Things like the shell and the filesystem commands (ls, cp, and so on) are stored here. The /bin directory usually doesn't receive modification after installation. If it does, it's usually in the form of package upgrades that we provide.

/boot

Files that are used by the Linux Loader (LILO). This directory also receives little modification after an installation.

/cdrom

Remember that all drives have to be mounted to a directory on the main root directory? Well, /cdrom is provided as a mount point for your CD-ROM drive.

/dev

Everything in Linux is treated as a file, even hardware devices like serial ports, hard disks, and scanners. In order to access these devices, a special file called a device node has to be present. All device nodes are stored in the /dev directory. You will find this to be true across many UNIX-like operating systems.

/etc

This directory holds system configuration files. Everything from the X Window configuration file, the user database, to the system startup scripts. The system administrator will become quite familiar with this directory over time.

/home

Linux is a multiuser operating system. Each user on the system is given an account and a unique directory for personal files. This directory is called the user's “home” directory. The /home directory is provided as the default location for user home directories.

/lib

System libraries that are required for basic operation are stored here. The C library, the dynamic loader, the ncurses library, and kernel modules are among the things stored here.

/lost+found

When the system boots, the filesystems are checked for any errors. If errors are detected, the fsck program is run to see if any can be corrected. The corrected parts of the filesystem are written to the /lost+found directory.

/mnt

This directory is provided as a temporary mount point for working on hard disks or removable drives.

/opt

Optional software packages. The idea behind /opt is that each software package installs to /opt/<software package>, which makes it easy to remove later. Linux distributes some things in /opt (such as KDE in /opt/kde), but you are free to add anything you want to /opt.

/proc

This is a unique directory. It's not really part of the filesystem, but a virtual filesystem that provides access to kernel information. Various pieces of information that the kernel wants you to know are conveyed to you through “files” in the /proc directory. You can also send information to the kernel through some of these “files”. Try doing cat /proc/cpuinfo.

/root

The system administrator is known as “root” on the system. root's home directory is kept in /root instead of /home/root. The reason is simple. What if /home was a different partition from / and it could not be mounted? root would naturally want to log in and repair the problem. If his home directory was on the damaged filesystem, it would make it difficult for him to log in.

/sbin

Essential programs that are run by root and during the system bootup process are kept here. Normal users will not run programs in this directory.

/tmp

The temporary storage location. All users have read and write access to this directory.

/usrIntroduction to Linux/Unix

This is the big directory on a Linux system. Everything else pretty much goes here, programs, documentation, the kernel source code, and the X Window system. This is the directory to which you will most likely be installing programs.

/var

System log files, cache data, and program lock files are stored here. This is the directory for frequently-changing data.unix


2.  File Types

There are four types of files in the Unix file system.

2.1  Ordinary Files

An ordinary file may contain text, a program, or other data. It can be either an ASCII file, with each of its bytes being in the numerical range 0 to 127, i.e. in the 7-bit range, or a binary file, whose bytes can be of all possible values 0 to 255, in the 8-bit range.

2.2  Directory Files

Suppose that in the directory x I have a, b and c, and that b is a directory, containing files u and v. Then b can be viewed not only as a directory, containing further files, but also as a file itself. The file b consists of information about the directory b; i.e. the file b has information stating that the directory b has files u and v, how large they are, when they were last modified, etc.1

2.3  Device Files

In Unix, physical devices (printers, terminals etc.) are represented as ``files.'' This seems odd at first, but it really makes sense: This way, the same read() and write() functions used to read and write real files can also be used to read from and write to these devices.

2.4  Link Files

Suppose we have a file X, and type

ln X Y
If we then run ls, it will appear that a new file, Y, has been created, as a copy of X, as if we had typed
cp X YIntroduction to Linux/Unix
However, the difference is the cp does create a new file, while ln merely gives an alternate name to an old file. If we make Y using ln, then Y is merely a new name for the same physical file X.

3.  Obtaining Information About the Files in a Given Directory

The `a' (``all'') and `l' (``long'') options of the ls command will give us a lot of information about files in a specified directory (if we don't specify a directory, then the current directory is assumed). Here is a sample output from typing


root@gaurav:/ # ls -la
total 60
drwxr-xr-x   24  root root   640   2005-04-14 12:00 .
drwxr-xr-x   24  root root   640   2005-04-14 12:00 ..
drwxr-xr-x    2   root root  2320  2005-04-16 01:19 bin
drwxr-xr-x    3   root root   696   2005-04-16 10:05 boot
drwxr-xr-x   12  root root 14280 2005-04-16 10:08 dev
drwxr-xr-x   11  root root 39136  2005-04-14 13:05 .dev
drwxr-xr-x  116 root root  6432  2005-04-16 10:15 etc
drwxr-xr-x    3   root root    72    2005-04-14 04:59 home
drwxr-xr-x   15  root root  4376  2005-04-14 16:11 lib
drwxr-xr-x   10  root root   240   2005-04-15 23:18 mnt
drwxr-xr-x    2   root root    48    2005-04-14 04:50 opt
dr-xr-xr-x  118   root root     0     2005-04-16 15:37 proc
drwxr-xr-x   27  root root  1544  2005-04-16 10:14 root
drwx------     2    root root    48   2005-04-14 04:48 .reiserfs_priv
drwxr-xr-x    2   root root  6752  2005-04-16 01:19 sbin
drwxr-xr-x    2   root root    48    2005-04-14 04:50 srv
drwxr-xr-x   10  root root     0     2005-04-16 15:37 sys
drwxrwxrwt 11 root root   416  2005-04-16 10:35 tmp
drwxr-xr-x   12  root root   312  2005-04-14 04:51 usr
drwxr-xr-x   16  root root   384  2005-04-14 13:09 var


Suppose you want to get a listing of the hidden files in the current directory. This command will do just that:

root@gaurav:/ # ls -a
.    boot   .dev    plugins   root  sys  var
..   cdrom  etc   mnt    proc sbin  tmp 
bin  dev    home  lib         opt    .reiserfs_priv  srv   usr

The output is separated into six columns:
1st column - access permissions 
2nd column - number of file entries (in the case of directory files)
3rd column - owner
4th column - size in bytes
5th column - date and time of last modification
6th column - name

Files beginning with a period (called “dot files”) are “hidden” when you run ls. You will only see them if you pass the -a option.

There are many more options that can be found in the online manual page. Don't forget that you can combine options that you pass to ls

Finding Files

You now know what each directory holds, but it still doesn't really help you find things. I mean, you could go looking through directories, but there are quicker ways. There are four main file search commands available in Slackware.

which

The first is the which(1) command. which is usually used to locate a program quickly. It just searches your PATH and returns the first instance it finds and the directory path to it. Take this example:


 $ which bash
/bin/bash

From that you see that bash is in the /bin directory. This is a very limited command for searching, since it only searches your PATH.

whereis

The whereis(1) command works similar to which, but can also search for man pages and source files. A whereis search for bash should return this:


$ whereis bash
bash: /bin/bash /usr/bin/bash /usr/man/man1/bash.1.gz

This command not only told us where the actual program, but also where the online documentation is stored. Still, this command is limited. What if you wanted to search for a specific configuration file? You can't use which or whereis for that.

find

The find(1) command will search for anything. I want to search the entire system for the default xinitrc file on the system.


 $ find / -name xinitrc
./var/X11R6/lib/xinit/xinitrc

find will take a while to run, since it has to traverse the entire root directory tree. And if you run this command as a normal user, you will probably get permission denied error messages for directories that only root can see. But find found our file, so that's good. If only it could be a bit faster...

locate

The locate(1) command searches the entire filesystem, just like the find command can do, but it searches a database instead of the actual filesystem. The database is set to automatically update at 4:40AM, so you have a somewhat fresh listing of files on your system. You can manually run updatedb(1) to update the locate database (before running updatedb by hand, you must first su to the nobody user). Here's an example of locate in action:


 $ locate xinitrc   
/var/X11R6/lib/xinit/xinitrc
/var/X11R6/lib/xinit/xinitrc.fvwm2
/var/X11R6/lib/xinit/xinitrc.openwin
/var/X11R6/lib/xinit/xinitrc.twm

We got more than what we were looking for, and quickly too. With these commands, you should be able to find whatever you're looking for on your Linux system.


References:
http://slackware.com
http://heather.cs.ucdavis.edu/~matloff/UnixAndC/Unix/FileSyst.html



http://www.linux-iips.tk