CHAPTER 2
The Linux directory structure is like a tree. The base of the Linux file system hierarchy begins at the root, or trunk, and directories branch off from there. Each one of these directories, called folders on other operating systems, can and often do contain other directories. The directories on a Linux system are separated by a forward slash.
What follows is a list of some of the most important top-level directories. Of course, all of the directories on a Linux system have a purpose, but understanding what these particular directories are for is rather important as a user of a Linux system. These top-level directories will be the ones that you interact with most often.
Every file and directory on a Linux system resides under the / directory. This directory is referred to as the root directory or sometimes "slash," a shorthand way of saying forward slash.
Even additional physical or virtual storage devices that are attached to a Linux system live somewhere underneath the / directory. The C:\ drive on a Windows system is analogous to / on Linux. When another storage device is attached to a Windows system, it is assigned a new drive letter such as D:\. On a Linux system, storage devices are attached, or mounted, to a directory such as /mnt or /media/external.
The /bin directory houses essential user binaries and other executable programs. The most basic and fundamental command line utilities reside in /bin. For example, some of the commands in /bin are used to list, copy, move, and view files. Other non-essential binaries are located in /usr/bin. You will find graphical applications such as web browsers and mail readers there, as well as various other command line utilities.
Configuration files that control how applications or the operating system behave are located in the /etc directory. For example, there is a configuration file in /etc that tells the operating system whether to boot into a text mode or a graphical mode.
Each user on a Linux system has a subdirectory dedicated to his or her account in the /home directory. For example, my user account is "jason" and thus my home directory is /home/jason. Since all users have their own home directory, they have the option of keeping their data private, sharing it with other users on the system, or a combination of the two.
Typical home directory contents include files created by the user, text documents, vacation pictures, music, etc. Additionally, user-specific configurations are stored in the home directory. These configuration files can control the behavior of the user's graphical or text environment, for example.
Optional or third-party software resides in the /opt directory. The /opt directory is for software that is not bundled with the operating system. For example, the Google Chrome web browser is not part of the standard Linux operating system and installs in /opt/google/chrome.
Temporary space is available in /tmp. This directory can be used by applications or by individual users on the system. The contents of /tmp are typically cleared at boot time, so do not store anything in /tmp that you can't live without or that you want to store long-term.
The /usr directory is where user-related programs and read-only data reside. The contents of /usr are meant to be used by actual users of the system as opposed to the operating system itself. A whole directory hierarchy exists in /usr. For example, the /usr/bin directory contains binary files and applications, while /usr/share/doc contains documentation related to those applications.
Variable data, the most notable being log files, is stored in the /var directory. Several log files exist in the /var/log directory or a subdirectory thereof.
In addition to the directories previously covered, there are additional top-level directories you may encounter on a Linux system. Many of these directories will be of little concern to you in your day-to-day use of the operating system; however, they are an essential part of a functioning Linux system. This may be used as a quick reference to help you understand the general purpose of each of these top-level directories. Some subdirectories are included in this list to help clearly define the purpose of the top-level directory.
/ The starting point of the Linux file system hierarchy, called the root directory.
/bin Binaries and other executable programs.
/boot Files required to boot the operating system.
/cdrom Where CD-ROMs are attached or mounted.
/cgroup Control groups hierarchy.
/dev Device files, typically controlled by the operating system and the system administrators.
/etc System configuration files.
/home User home directories.
/lib System libraries.
/lib64 System libraries, 64-bit.
/lost+found Used by the file system to store recovered files after a file system check has been performed.
/media Used to mount removable media like USB drives.
/mnt Used to mount external file systems.
/opt Optional or third-party software.
/proc Process information virtual file system.
/root The home directory for the root (superuser) account.
/sbin System administration binaries.
/selinux Virtual file system used to display information about SELinux.
/srv Contains data which is served by the system.
/srv/www Web server files.
/srv/ftp FTP files.
/sys Virtual file system used to display and sometimes configure the devices and buses known to the Linux kernel.
/tmp Temporary space, typically cleared on reboot.
/usr User-related programs, libraries, and documentation.
/usr/bin Binaries and other executable programs.
/usr/lib Libraries.
/usr/local Locally installed software that is not part of the base operating system.
/usr/sbin System administration binaries.
/var Variable data, most notably log files.
/var/log Log files.
If you encounter other top-level directories that have not been listed here, those were more than likely created by the system administrator.
Application directory structures can be patterned after the operating system. Here is a sample directory structure of an application named apache installed in /usr/local.
/usr/local/apache/bin The application's binaries and other executable programs.
/usr/local/apache/etc Configuration files for the application.
/usr/local/apache/lib Application libraries.
/usr/local/apache/logs Application log files.
If apache were to be installed in /opt it would look like this:
/opt/apache/bin The application's binaries and other executable programs.
/opt/apache/etc Configuration files for the application.
/opt/apache/lib Application libraries.
/opt/apache/logs Application log files.
Another common application directory structure pattern includes moving the configuration and variable data outside of /opt. Instead of placing all of the application components in /opt/app-name, /etc/opt/app-name is used for configuration files and /var/opt/app-name is used for logs. Continuing with the apache application example, here is a demonstration of this method:
/etc/opt/apache Configuration files for the application.
/opt/apache/bin The application's binaries and other executable programs.
/opt/apache/lib Application libraries.
/var/opt/apache Application log files.
Not only can applications be segregated into their own directories, they can share a common directory structure with other applications that are not part of the standard operating system. For example, apache can be directly installed into /usr/local. In this case, the binaries would reside in /usr/local/bin, while the configuration would reside in /usr/local/etc. Since apache may not be the only locally installed application, it could share that space with the other programs.
Directory structures can be based on an organization such as a company, group, or team. For example, if you work for the Widget corporation, you may find a directory named /opt/widget or /usr/local/widget on the company's Linux servers. In some cases, this base directory is treated much like an application directory. It will contain common subdirectories like /etc and /bin. Here is an example:
/opt/widget The top-level directory for the Widget company.
/opt/widget/bin Binaries and programs installed or created by the Widget company.
/opt/widget/etc Configuration files for the programs installed or created by the Widget company.
Further subdivisions can be made within this organizational directory structure. For instance, each application may receive its own subdirectory as follows:
/opt/widget The top-level directory for the Widget company.
/opt/widget/apache The top-level directory for the Widget company's installation of apache.
/opt/widget/apache/bin The apache binaries.
/opt/widget/apache/bin The apache configuration files.
Here are variations on the same theme, but based on a team within the company.
/opt/sysadmin The system administrator team's top-level directory.
/opt/widget/sysadmin The system administrator team's top-level directory.
/usr/local/widget/sysadmin The system administrator team's top-level directory.