Linux Directory Structure and Filesystem Hierarchy

horizontal tree view of file hierarchy

If you are new to Linux, and just started using it, you might have heard some terms like var, root, bin, etc and many others. Today we will talk about these terms and try to clear your confusion.

var, root, bin, etc are actually different directories on your Linux distribution. The directory structure in Linux is quite different from the directory structure in Windows. In Windows, we see that most of the programs are being installed in a directory named Program Files and system files are in system32.

In Linux, the file hierarchy is totally different. There is a “Filesystem Hierarchy Standard (FHS)” maintained by Linux foundation that defines the structure and the content of directories in all UNIX based systems. Because of maintaining this standard, almost all the Linux distros have the same directory structure.

In this article, we will learn the Linux directory structure in detail and try to understand the meaning and contents of different directories.

Linux Directory Structure

word image 2

Linux File Hierarchy

Linux directory structure follows the structure of a tree. The initial or the core part of a tree is the root. root or / is the main (or core) directory of the filesystem. All the other files and directories appear under this root or / directory.

The root directory is denoted by the symbol /. In Linux, everything, which includes files and directories, is located under root, which is represented by this /.

For example, there can be a directory var, inside it there is another named log. So, the tree structure will start from root and it will look like:

root -> var -> log

Now when you examine the directory structure, you’ll notice that it resembles the root of a plant – For example, now the log directory inside the var directory will be denoted as:

/var/log

First / resembles the root. Under that there lies the /var, and under that /var there lies /log. All directories have / before them as per standard, apart from the root as it is by default denoted by /.

Directories in Linux File Hierarchy

We will cover some common directories and their contents in brief.

/lib – System Library Files

Shared libraries for executing binaries are stored here.

Libraries are just some code or you can think of modules that executable binaries can use. The libraries that are mostly required by binaries in the /bin and /sbin folders, are stored in this /lib directory.

/bin – User Binary Files

Binaries are referred to as bins. Many basic shell commands, such as ls, cp, and cd, are directly contained in the bin directory.

Programs that we write are in human readable code. These programs or commands are converted and compiled into binaries as machines can only read binaries. Here in the bin directory, you will find all these binaries.

/dev – Device Files

Device files are stored in the /dev directory.

Only specific files, such as those related to the devices, are stored here. These are virtual files that do not exist on the hard drive. Many of them are mainly generated at boot time. For example, if you enter a new device like a pen drive, a new device entry will be stored here in dev. There exists a directory named /dev/null, known as null device. If you write anything here, that will permanently be deleted forever.

Configuration files are located in the /etc directory.

This /etc is one of the most important directories in the Linux system. The /etc directory includes all the system’s essential configuration files, such as the password file and networking files, which are used primarily by the administrator and services. There will be many times when you will need to change some configuration of a particular program. You will then file the configuration or .conf file here under the /etc directory.

/home – User’s Home Directory

Users’ personal directories are found in the home directory.

The user data and user-specific configuration files are stored in the home directory. All of your personal files, notes, applications and everything will be stored in your home directory as a user. It is a common practice to create a home directory while creating a user on a Linux system. But you can also create a user without their own home directory.

Another important thing to understand. Let us think we have two users on your Linux system named abc and xyz. Now as per file system standard, they’ll have their own home directories at /home/abc and /home/xyz, accordingly.

The user should be the one who has the accessibility to his or her location. And so xyz will have access only to his home /home/xyz and will have no access to /home/abc.

/root – Root Home Directory

The root’s home directory.

Don’t confuse /root with /. As a root user will always exist in the Linux system, so this user should also get a home directory of its own. As a result, instead of /home/root, root’s home is at /root.

/opt – Optional Applications

Optional applications and all their related files are stored here.

Traditionally, the /opt directory has been used to install third-party apps that aren’t available through the distribution’s repository.

/sbin – System Binary Files

Binary files for the system are stored here.

We already have learned about the /bin directory. The /sbin directory also contains binary, but here only those binary files are stored which are used by system only.

The main distinction between bin and sbin is that sbin includes binaries that only root or a sudo user may have permission.

/tmp – Temporary Files

Temporary files are stored in the /tmp directory.

This directory stores temporary files, as the title indicates and is used by many apps to store temporary files.

However, keep in mind that when your system restarts, the contents of the /tmp directories are erased. Some Linux systems will automatically erase old files, so normally we don’t save anything important here.

/usr – User Programs

User binaries and program data are stored in the /usr directory.

All executable files, libraries, and source code for most system programs are located under /usr. As a result, the majority of the files housed there are read only as they are for normal users.

Under /usr, there are many subdirectories. Like there is /usr/bin where basic user commands can be found and additional commands for the administrator can be found in /usr/sbin. The system libraries are located in /usr/lib.

/var – Variable Files

Variable data files are stored here.

Var means variable. In this directory data of different variables are stored. Here programs or operating systems save runtime data such as system logging, user tracking, caches files etc.

Because the files in this directory are not automatically cleaned, it is an excellent area for system administrators to look for information about their system’s behavior.

/boot – Boot Loader Files

Files that are used to start the computer are stored here.

The kernel and boot image files, as well as LILO and Grub, are all located in the /boot directory. Usually we mount the directory in the first partition of the disc.

Process and kernel files are located under the /proc directory.

The information about presently running processes and kernel parameters can be found in the /proc directory. Many tools use the contents of the proc directory to obtain runtime system information.

In Linux, for example, you may look for processor information by looking at the file /proc/cpuinfo.

/media – Removable Devices

This directory serves as a removable media mounting point.

When you attach a removable media device, such as a USB disk, SD card, or DVD, a directory under the /media directory is automatically created for it to access the media.

/mnt – Mount Directory

This directory serves as a temporary mounting point.

The /mnt directory and its subdirectories are designed to be used as temporary mounting points for storage devices. This directory is identical to /media, but instead of automatically mounting removable media, we can use /mnt to explicitly mount a filesystem there.

/srv – Service Data

Here the data for the services are stored.

The /srv directory stores data for the system’s services. This directory contains site-specific data served by your system. This directory gives users the location of data files for a particular service, such as FTP, WWW, or CVS.

For example, if we run an HTTP server, we will store website data under the /srv directory.

/sys – Virtual Filesystem

The /sys file is a kernel interface.

In particular, it gives a filesystem-like view of the kernel’s information and configuration options. This is not an actual filesystem, just a virtual thing to give you a view.

Conclusion

Hopefully this gives you some insights about the hierarchy of Linux filesystems. If you have any feedback or questions feel free to leave a comment and we’ll get back to you as soon as we can.

1 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
Bash For Loop
Read More

Bash For Loop

Loops are used in computer programming languages to repeat certain instructions several times. This is not only to…
Touch Command in Linux
Read More

Touch Command in Linux

Creating files is one of the most common things we do. Every now and then, you may also…