Monday, January 10, 2011

The Unix File System: A Simple Design

Coming over from the Windows platform to a Linux/Unix platform can be challenging. I learned quickly there was some unlearning to do. One of the fundamental differences between Windows and Unix is how both treat storage devices and directory layout. As a competent user with Windows you get accustomed to knowing where programs go and temporary data is stored. It feels natural over time for things to go where they go. When you see the Linux operating system, it all seems so foreign, so strange. Where is the C drive? Where is Documents and Settings? Where are my programs? Where is everything?! And when a seasoned Linux user tries to explain it to you it takes time to sink in.

Part of the confusion is how Linux and Windows deal with storage devices or partitions for that matter. In Linux everything and I do mean everything is a file or folder. That sounds profoundly normal, but it's not. On Windows your hard disks and partitions on those hard disks are given drive letters and treated as devices rather than files. How could a device ever be a file? It's not. However the description of how to talk to the device is contained in a special file. This special file is used for the mount command to take this storage device or partition and attach it to the directory structure. An example of this is I have the /media directory for such a purpose. Let us say I want to take my USB drive and mount it. It will appear as /dev/sdb1. I run the mount command and the contents of my USB drive will show up in /media. To show how flexible this design is, I can have five hard drives with 20 different partitions on each and have all fit nicely under the tree structure on Linux. In Windows this would not be easy to do if not impossible since there are only 26 drive letters. The design has some advantages.

Now we need to explain the directory structure. Everything comes under what we call the / (root) directory. Here we have the directories /bin, /boot, /dev, /etc, /home, /lib, /media, /mnt, /opt, /proc, /root, /sbin, /srv, /tmp, /usr, and /var. Windows users it seems foreign doesn't it? The moment you can admit that Linux is not like Windows and it is a whole different breed of operating system you will make progress. If you keep looking for ways to compare it to Windows, that will only hinder your progress in the transition. That said we can continue.

The directory structure is something like this: /bin holds some basic programs needed for repairing the system and bringing it up. Some of these programs list the contents of a directory, change directories, change permissions on a file, change ownership of a file, search for a specified file, provide the utilities for accepting commands, etc. Stuff most end users don't even see.

Next up is the /boot folder. This contains the different components needed to load the operating system on boot up. Here we have our boot loader which gives us the option to boot Linux. In this folder is contained the configuration for the boot menu like which operating system you would like to boot Linux, Windows, FreeBSD, or several flavors of Linux. The kernel is loaded from this partition in that an image of it is placed here. Normally this folder doesn't take any more than 100 MiB.

/dev holds all the files that describe how our devices work. We have a file that describes how to interface with the CD-ROM, how to interface with this partition and that partition, how to utilize the mouse, etc.

If you ever needed to change settings on a system that affect the whole system or turn on some options more than likely /etc is the place to do it. This is where all the configuration files are located for several different pieces of software. In the past if you wanted to add another resolution to your monitor (assuming it supports it) you would either use a graphical tool to do this or add it by hand, but all the same it made changes in the /etc/X11/xorg.conf file. Needless to say this is one of the folders used to tweak the system.

Whatever happened to Documents and Settings? Users can store their content in their account folder which is located in the /home directory. The user folders hold files that users create, copy, or download. It also houses user specific configuration files that make the settings I set specific to me and not everyone else. This is the folder you are going to be more acquainted with than any other.

/lib holds the different libraries that programs on the system use. These libraries contain functions (a programming element) that can be reused for different programs and even different purposes. /media is where the system mounts different file systems and devices. /mnt is used for temporary file systems.

/opt is sometimes used for certain programs that you install through a tarball or RPM. /proc holds information pertaining to the system and other things needed by the kernel. /root is the root users home directory, so it is just like having the root accounts folder in / instead of /home. /sbin is similar to /bin, but it contains more programs related to file systems, networking tools, and system administration to only be used by someone with root privileges. I have not figured out what /srv does exactly because on my system it is empty.

/tmp hold temporary files much like C:\Temp. /usr is a folder that shares a bunch of things. Here we have programs, documentation, source code, more program libraries, backgrounds, pictures, etc. This is similar to Program Files on Windows in a sense, but it is much broader than that. /var holds logs, messages, and job spools primarily.

So there you have it. Now you should have a rough idea of how the Linux/Unix file system functions. With this information you can diagnose problems more easily and find more information to help you. Again for more specific details type in man hier on the command line to view the layout page.

No comments:

Post a Comment