×

Search anything:

Linux file hierarchy

Free book on Graph Algorithms

Get this book -> Problems on Array: For Interviews and Competitive Programming

A file hierarchy is an organization of files in a storage device i.e hdd, ssd etc, in this article we discuss the file hierarchy of the Linux operating system.

Table of contents.

  1. Introduction.
  2. Binaries.
  3. Configuration directories.
  4. Data directories.
  5. Memory directories.
  6. Variable directories.
  7. System resources.
  8. Non-standard directories.
  9. Summary.
  10. References.

Introduction.

The Linux operating system has a hierarchical file structure. The FHS(file system hierarchical standard) defines the directory structure and its contents for the Linux operating system.

Standard Linux commands, directories, users, applications, kernel, library, temp files are all included in the file hierarchy and enable the OS to function properly.
This structure is in the form of a tree, to view this structure we can either navigate to root directory 'cd / && ls' or to view it more graphically we can use the tree utility which can be installed as follows.

Debian or Ubuntu

sudo apt install tree

Fedora or Redhat

sudo dnf install tree

After installation change directory to the root directory, cd / and type in tree command.

tree-1

Binaries.

Binary files contain compiled source code, also called executables since they can be executed.

The binary directories are;

  • /bin: Stores terminal core utilities and commands such as cat, chmod, chown, df, echo, ls, mkdir, pwd, rm, rmdir, kill, uname and more.
  • /sbin: This is similar to bin with the exception that it is dedicated to commands that can only be executed by a super user or root user. They include, fsck, halt, mkfs, mkswap, reboot, route, swapoff, swapon and more.
  • /lib: This stores libraries which are needed for programs to function. Libraries can be shared between different programs this is the main reason why Linux installations are lightweight.
  • /opt: Stores optional software which is not managed by the Linux's distribution package manager. That is, it will store add-on applications from individual vendors.

Configuration directories.

These directories will store configured files which are responsible for configuring the parameters and initial settings for computer programs.

They include,

/boot: This will store all needed files for booting. It is advisable not to modify this directory as it leads to booting problems if not configured right.

/etc: This is responsible for storing configuration files which affect all system users. It will mostly contain static program local files. A backup of this folder is essential as it will save us a lot of configuration during re-installation. An example of a sub-directory is the /etc/init.d which holds scripts that control the system or start and stop daemons.

Data directories.

As the name suggests these directories store system data, directories here include,
/home: This directory will store all the user's data since this is where most of the time is spent. Sub-directories such as desktop, documents, downloads, photos are all stored here.

This directory also contains personal configuration files dot-files since they are hidden. When there is conflict between personal config files and system config files, personal configs will succeed.

  • /root: This is the home directory for the system admin or root user. Note that the / directory is different from this directory.
  • /srv: It is responsible for storing data for services e.g www, ftp currently running on the system.
  • /media: This is a directory that provides a place where external devices such as optical drives, USBs can be mounted. It can vary between Linux distributions. It is managed by the operating system, if you need to mount a device manually the /mnt directory serves that purpose.
  • /mnt: This is a placeholder directory to store other mounted folders or drives such as external HDDs, Floppy disks or USBs. /media directory is managed by the os and can be used in place of this however /mnt can also be used for manually mounting devices.
  • tmp: Temporary files of the running applications are stored here, and unlike in the windows OS, they are usually deleted upon shutdown. Data stored here can either use disk space or RAM.

Memory directories.

These directories contain files of the whole system, files storing information like device information, processes data and all system related information.

Sub-directories include,

  • /dev: Physical devices such as HDDs, USBs, optical drives are mounted here. That is, the systems HDD might be mounted on /dev/sda and an external USB on /dev/sde/ also there may exists different partitions, i.e /dev/sda1, /dev/sda2/.
  • /proc: This is the processes directory where system information is represented as files. This acts as a way for the kernel to communicate with processes running in the system. It can also be seen as a virtual file system since it only contains runtime system information(not 'real' files).
  • /sys: This is a virtual directory like /dev/ created during bootup. It holds all information related to all connected devices.

Variable directories.

The directory here is the var directory used to store all variable data i.e system logs. Files and folders here are expected to increase with the passing of time and system usage.

Some of the subdirectories here include,

  • /var/log: This will contain all log files.
  • /var/lib: Files containing state information i.e databases are stored here.
  • /var/cache: Stores all cached data from applications usually generated by I/O or calculations. Files here can be deleted without any loss of data.
  • /var/spool: This stores files awaiting processing i.e mail queues.

System resources.

The only directory here is the /usr (unix system resources) which will have all files and utilities shared between users. It will have its own /bin, /sbin/ and /lib directories different from the sudoer's directories.

Sub-directories of /usr are,

  • /usr/bin: which contains non-important global binary commands for users.
  • /usr/lib: which has all binaries for usr/bin, /usr/sbin not directly executed by users.
  • /usr/include: this contains all standard C include files.
  • /usr/share: Which contains shared data.
  • /usr/local: programs installed from source are stored here.
  • /usr/src: which stores source code e.g kernel source code with its header files.

Non-standard directories.

This directories are not included in the FHS, they include,

  • /cdrom: mounting can occur in the /media directory however, cdroms can also be mounted here.
  • /run: Run-time variable data i.e data about system since last boot, is stored here.
  • /lost+found: When a system error occurs, e.g crashing, lost data recovered by fsck(file system checker) is stored here. It might or might not be corrupted.

Summary.

The Linux File Hierarchy Standard is responsible for defining directory structure and contents, It is maintained by the Linux Foundation.

Keep in mind that in a UNIX/Linux system everything is a file otherwise it is a process

References.

  1. File systems and hierarchy (PDF) by Professor Stewart Weiss from The City University of New York (CUNY), USA
  2. The Linux file system.
Linux file hierarchy
Share this