Oct 31, 2008

The linux file system explained

The first thing that most new users shifting from Windows will findconfusing is navigating the Linux filesystem. The Linux filesystem does things a lot more differently than the Windows filesystem.This article explains the differences and takes you through the layout of the Linux filesystem. 

For starters, there is only a single hierarchal directory structure.Everything starts from the root directory, represented by '/', and thenexpands into sub-directories. Where DOS/Windows had various partitions andthen directories under those partitions, Linux places all the partitionsunder the root directory by 'mounting' them under specific directories.Closest to root under Windows would be c:. 

Under Windows, the various partitions are detected at boot and assigned adrive letter. Under Linux, unless you mount a partition or a device, thesystem does not know of the existence of that partition or device. Thismight not seem to be the easiest way to provide access to your partitionsor devices but it offers great flexibility. 

This kind of layout, known as the unified filesystem, does offer severaladvantages over the approach that Windows uses. Let's take the example ofthe /usr directory. This directory off the root directory contains most ofthe system executables. With the Linux filesystem, you can choose to mountit off another partition or even off another machine over the network. Theunderlying system will not know the difference because /usr appears to bea local directory that is part of the local directory structure! How manytimes have you wished to move around executables and data under Windows,only to run into registry and system errors? Try moving c:windowssystemto another partition or drive. 

Another point likely to confuse newbies is the use of the frontslash '/'instead of the backslash '' as in DOS/Windows. So c:windowssystem wouldbe /c/windows/system. Well, Linux is not going against convention here.Unix has been around a lot longer than Windows and was the standard a lotbefore Windows was. Rather, DOS took the different path, using '/' forcommand-line options and '' as the directory separator. 

To liven up matters even more, Linux also chooses to be case sensitive.What this means that the case, whether in capitals or not, of thecharacters becomes very important. So this is not the same as THIS or ThIsfor that matter. This one feature probably causes the most problems fornewbies. 

We now move on to the layout or the directory structure of the Linuxfilesystem. Given below is the result of a 'ls -p' in the root directory. 

bin/ dev/ home/ lost+found/ proc/ sbin/ usr/boot/ etc/ lib/ mnt/ root/ tmp/ var/ 

/sbin - This directory contains all the binaries that are essential to theworking of the system. These include system administration as well asmaintenance and hardware configuration programs. Find lilo, fdisk, init,ifconfig etc here. These are the essential programs that are required byall the users. Another directory that contains system binaries is /usr/sbin. This directory contains other binaries of use to the system administrator. This is where you will find the network daemons for your system along with other binaries that only the system administrator has access to, but which arenot required for system maintenance, repair etc. 

/bin - In contrast to /sbin, the bin directory contains several usefulcommands that are used by both the system administrator as well asnon-privileged users. This directory usually contains the shells likebash, csh etc. as well as much used commands like cp, mv, rm, cat, ls. There also is /usr/bin, which contains other user binaries. These binaries on the other hand are not essential for the user. The binaries in /bin however, a user cannot do without. 

/boot - This directory contains the system.map file as well as the Linuxkernel. Lilo places the boot sector backups in this directory. 
/dev - This is a very interesting directory that highlights one importantcharacteristic of the Linux filesystem - everything is a file or adirectory. Look through this directory and you should see hda1, hda2 etc,which represent the various partitions on the first master drive of thesystem. /dev/cdrom and /dev/fd0 represent your CDROM drive and your floppydrive. This may seem strange but it will make sense if you compare thecharacteristics of files to that of your hardware. Both can be read fromand written to. Take /dev/dsp, for instance. This file represents yourspeaker device. So any data written to this file will be re-directed toyour speaker. Try 'cat /etc/lilo.conf > /dev/dsp' and you should hear somesound on the speaker. That's the sound of your lilo.conf file! Similarly,sending data to and reading from /dev/ttyS0 ( COM 1 ) will allow you tocommunicate with a device attached there - your modem. 

/etc - This directory contains all the configuration files for your system.Your lilo.conf file lies in this directory as does hosts, resolv.conf andfstab. Under this directory will be X11 sub-directory which contains theconfiguration files for X. More importantly, the /etc/rc.d directorycontains the system startup scripts. This is a good directory to backupoften. It will definitely save you a lot of re-configuration later if youre-install or lose your current installation. 

/home - Linux is a multi-user environment so each user is also assigned aspecific directory which is accessible only to them and the systemadministrator. These are the user home directories, which can be foundunder /home/username. This directory also contains the user specificsettings for programs like IRC, X etc. 

/lib - This contains all the shared libraries that are required by systemprograms. Windows equivalent to a shared library would be a DLL file. 

/lost+found - Linux should always go through a proper shutdown. Sometimesyour system might crash or a power failure might take the machine down.Either way, at the next boot, a lengthy filesystem check using fsck willbe done. Fsck will go through the system and try to recover any corruptfiles that it finds. The result of this recovery operation will be placedin this directory. The files recovered are not likely to be complete ormake much sense but there always is a chance that something worthwhile isrecovered. 

/mnt - This is a generic mount point under which you mount your filesystemsor devices. Mounting is the process by which you make a filesystemavailable to the system. After mounting your files will be accessibleunder the mount-point. This directory usually contains mount points orsub-directories where you mount your floppy and your CD. You can alsocreate additional mount-points here if you want. There is no limitation tocreating a mount-point anywhere on your system but convention says thatyou do not litter your file system with mount-points. 

/opt - This directory contains all the software and add-on packages thatare not part of the default installation. Generally you will find KDE andStarOffice here. Again, this directory is not used very often as it'smostly a standard in Unix installations. 
/proc - This is a special directory on your system. We have a more detailedarticle on this one here.
/root - We talked about user home directories earlier and well this one isthe home directory of the user root. This is not to be confused with thesystem root, which is directory at the highest level in the filesystem. 
/tmp - This directory contains mostly files that are required temporarily.Many programs use this to create lock files and for temporary storage ofdata. On some systems, this directory is cleared out at boot or atshutdown. 

/usr - This is one of the most important directories in the system as itcontains all the user binaries. X and its supporting libraries can befound here. User programs like telnet, ftp etc are also placed here./usr/doc contains useful system documentation. /usr/src/linux contains thesource code for the Linux kernel.

/var - This directory contains spooling data like mail and also the outputfrom the printer daemon. The system logs are also kept here in/var/log/messages. You will also find the database for BIND in /var/namedand for NIS in /var/yp. 
This was a short and basic look at the Linux filesystem. You do need tohave at least this basic knowledge of the layout of the filesystem tofully utilize its potential. One good place to read about the filesystemis this detailed document at
www.pathname.com/fhs/1.2/fsstnd-toc.html thatspecifies the standard structure of the Linux filesystem.