Friday 27 June 2014

Exploring the Linux File Structure

Okay, so I hope you installed Linux at least as a virtual OS in your machine. Now let us explore our OS.
For understanding any OS the first thing we must get to know is its file structure. There is a big difference in the Windows file structure and the Unix ( and hence Linux ) file structure.

In Windows, the different partitions are assigned a label, say C:, D:  etc and are called “Drives”. Now in a drive you make folders and store your data there. The C: drive is where the Windows OS is installed.

Linux, on the other hand, maintains a “tree” directory structure, where the base is the root directory ( folders are better known as directories here ) and all other directories are either directly or indirectly linked to this root directory. In easy words, imagine in your hard drive, Linux makes a folder for root, named as "/", and it contains some basic folders, say A, B, C, etc. Now in these folders, many other sub-folders are there. Thus the entire structure resembles a tree.

Windows uses “\” to separate folders in a path name.
Linux uses “/” for the same purpose (recall websites also follow this convention)

Before moving further, here are some basic symbols that you always need to keep in mind:

/ (forward slash)  - represents the root directory, i.e., the top most directory of the file system. It is also used to separate directories in a path name.
~ (tilde)           - represents the home directory of a user
. (period)           - represents your current directory
.. (double period)   - represents the parent directory of the current directory

Now as we are all set, let us do a study of the root directory. The root directory or '/' in general, contains the following directory. Each directory has a name indicating its purpose. Click on the picture for a clear view:



1. /bin        : bin stands for “Binary files” or simply “Binaries”. This directory contains the executable files, what we know as “.exe” files in Windows. This  directory contains the common OS executables which are used by all users. Example are the cp (copy) command, the mv (move) command, etc.
2. /sbin        : sbin stands for “System Binaries”. It contains the system level commands, for example, fdisk ( shows and manipulates partition tables ), mkfs (command to make files system), ifconfig ( to manage network interface, similar to ipconfig in Windows), etc.
3. /etc        : It stores configuration files for different programs. Windows uses the registry, which is a database to store configuration information for the programs. Linux simply uses text files with a '.conf' extension for this purpose. This directory thus stores the “config” files. When the Unix directory structure was designed, /etc was supposed to store extra files ( hence the name  'etc' ) . So the config and other files were pushed to this directory.  But now, this directory only stores config files.
There may be other explanations also why this directory was named “etc”.
4. /dev        : It stores files containig the information about various devices in your machine and the OS, and hence the name.
5. /proc        : proc stands for “process”. Thus this directory stores information about the various processes running in your system. For example, /proc/<pid> will contain information about the process with id <pid>
6. /var        : var directory stores variable content, e.g. , it stores log files, downloaded packages and database files, mails to the system, etc.
7. /tmp        : temporary files are stored here. This directory content is deleted when the system reboots. So do not store anything in this directory. It is similar to Temp folder in Windows.
8. /usr         : usr stands for Unix System Resources. It stores user level programs, libraries etc. We will come to this later.
9. /home        : It stores the files and sub-directories for different users,i.e , your personal files and directories are stored here. It is similar to "C:\Users\" folder in Windows.
10. /boot         : Stores files necessary for the OS to boot. Thus it contains the boot loader (GRUB files in Linux). Don't mess with it, or you are doomed!
11. /lib        : lib stands for “libraries”. It starts for various shared libraries. Windows shared libraries have .dll extension. Linux uses .so extension for such files. We sill see them later in a bit more detail.
12. /opt        : It is to store optional softwares and files. If you install some software in Linux manually, you may install them here.
13. /mnt        : File systems can mounted here. We will come to this later.
14. /media    : Removable devices are mounted here,e.g., USB drives, external HDDs, etc.
15. /srv        : It stores service data.
16. /lost+found    : In Linux, every partition has a lost+found directory. Files saved during failures are store in this directory.
17. /root        : Not to be confused with the root directory ( “/” ). This is the home directory for the root user ( Administrator in Windows).


Apart from the above principle directories, some systems may have other directories too. Feel free to explore them.

Now as we are familiar with the basic directory structure, we can now make a comparative study of some basic Windows and Linux folders. Before that, have a look at my '/' directory :



usr, bin and sbin directories:

“usr” stands for “Unix System Resources”. Before the /home directory came, this was used to store user files. This may be another possible explanation for its name. Now, it stores files and directories for various application softwares, while personal user files are stored in /home.

“bin” directory stores general executables, and sbin stores system or admin level executables. Open the terminal and type in the following command:

echo $PATH

You will get an output similar to below

/usr/local/sbin: /usr/local/bin: /usr/sbin: /usr/bin: /sbin: /bin:
As you can see, there are different “bin” and “sbin” folders at various locations.They are:

/bin    : The directory to store user level basic OS commands, like command for copying, moving, listing directories, etc. In short, the general terminal commands are stored here.
/usr/bin    : It stores default application softwares installed by the system. For example, executables for you mail reader, Firefox web browser, text editor, office suite, calculator, picture viewer, settings manager (similar to Control Panel in Windows), etc.
/usr/local/bin    : The local directory is used to store contents which are not default, and hence the “/usr/local/bin” directory is to store executables for softwares that are manually installed by the user.

If we are installing some software at our own wish, then we must never install them in /usr/bin as they may get deleted without any warning when we update or upgrade our system.

In a nutshell, take /usr/local/bin and /usr/bin as C:\ProgramFiles\ and C:\ProgramFiles(x86)\ in Windows.
The sbin folders store more important software executables, meant for system management, user management etc. They are generally at “/sbin” , “/usr/sbin” and “usr/local/sbin”.

lib directories:

lib folders store shared library files, similar to .dll files in Windows.
/lib stores the system libraries, similar to C:\Windows\System32 folder in Windows.
Other lib folders may store libraries for respective application softwares.

mnt and media directories:

Both are used to mount file systems. The /mnt folder is used to mount file systems manually by the user generally from the terminal. Whereas /media is for those external file systems which automatically get mounted by the system. Thus if you plug in a pendrive with name “My_USB”, you can access its contents from /media/My_USB/
Recall again that unlike Windows, Linux doesn't have so called “drives”. Here the removable devices are “mounted” on various directories. The default are /media/ and /mnt/ directories.

The home directory:

Also represented by '~' , it is the personal directory for the  users in the system. Take it similar to C:\Users\ in Windows.
Suppose there are two users with respective usernames “alpha” and “beta”. Then the /home directory will contain two subdirectories, “/home/alpha” and “/home/beta”. Each of these subdirectories will contain directories like “Documents”, “Music”, “Videos”, “Pictures”, etc. , similar to “My Documents”, “My Music” , “My Pictures” and “My Videos” in Windows where the users will store there stuff.

No comments:

Post a Comment