Do you know the Linux File-system Hierarchy?

Reading Time: 6 minutes

This article explains the Linux file system hierarchy and identifies the most important directories.

The File-system Hierarchy

The Linux system stores all files on file systems, which are organized into a single inverted tree known as a file-system hierarchy. This hierarchy is an inverted tree because the tree root (/) is at the top, and the branches of directories and subdirectories stretch below the root:

It’s essentially the directory structure you see starting from / (the root directory). This structure is defined by the FHS (Filesystem Hierarchy Standard), which many Linux distributions follow.

So, what is the Filesystem Hierarchy Standard (FHS)?

The FHS (Filesystem Hierarchy Standard) is a set of guidelines that defines how the directory structure and file placement should be organized on a Linux or UNIX-like operating system.
It’s basically the “rulebook” for where things go in the filesystem.

Purpose of FHS:

  • To ensure consistency across different Linux distributions.
  • To make it easier for users and administrators to navigate directories.
  • To help developers know where to install software files.
  • To allow scripts and programs to run reliably across different Linux systems.

What It Defines:

  • The names, purposes, and contents of the directories under /.
  • Which files/directories are mandatory and which are optional.
  • Where binaries, libraries, configuration files, logs, and temporary files must be located.

Example rules from FHS:

  • Essential binaries must be in /bin and /sbin so the system can boot and repair itself even if /usr is not mounted.
  • Configuration files go under /etc, not mixed with binaries.
  • User home directories are under /home.

Version:

  • The latest official version is FHS 3.0 (released in 2015 by the Linux Foundation).

Benefit for System’s Administrators:

Because most Linux distributions (like RHEL, Ubuntu, Debian, SUSE) follow FHS, you can:

  • Easily find logs in /var/log.
  • Always know that user home directories are under /home.
  • Know that configuration files live under /etc.

Top-Level Directories

Here’s a breakdown of the most important directories and what they’re for:

LocationPurpose
/bootFiles to start the boot process.
/devSpecial device files that the system uses to access hardware.
/etcSystem-specific configuration files.
/homeHome directory, where regular users store their data and configuration files.
/rootHome directory for the administrative superuser, root.
/runRuntime data for processes that started since the last boot. This data includes process ID files and lock files. The contents of this directory are re-created on reboot. This directory consolidates the /var/run and /var/lock directories from earlier versions of Red Hat Enterprise Linux.
/tmpA world-writable space for temporary files. Files that are not accessed, changed, or modified for 10 days are deleted from this directory automatically. The /var/tmp directory is also a temporary directory, in which files that are not accessed, changed, or modified in more than 30 days are deleted automatically.
/usrInstalled software, shared libraries, including files, and read-only program data. Significant subdirectories in the /usr directory include the following commands:
/usr/bin: User commands
/usr/sbin: System administration commands
/usr/local: Locally customized software
/varSystem-specific variable data should persist between boots. Files that dynamically change, such as databases, cache directories, log files, printer-spooled documents, and website content, might be found under the /var directory.

Key Concepts:

  • Everything starts at / (root).
  • Devices and processes appear as files (this is why Linux says “everything is a file”).
  • bin = binaries (programs), lib = libraries, etc = config.
  • /usr Historically meant “user system resources,” but is now mainly for system-wide software and libraries.
  • /var Changes frequently, while /usr and /etc are mostly static.

Absolute Path versus Relative Path

The path of a file or directory specifies its unique file-system location. Following a file path traverses one or more named subdirectories, which are delimited by a forward slash (/), until the destination is reached. Directories, also called folders, can contain other files and other subdirectories. Directories are referenced in the same manner as files.

Absolute Path

An absolute path gives the full path from the root directory / to the file or directory.
It always starts with /.
It’s like giving someone your full address (country → city → street → house number).

Key Points:

  • Always starts with /
  • Works no matter where you are in the filesystem
  • It is unique for each file/directory

Examples:

  • /home/thor/file.txt → file named file.txt inside Thor’s home directory
  • /etc/ssh/sshd_config → SSH server config file
  • /var/log/syslog → system log file
  • /usr/bin/python3 → Python interpreter
Relative Path

A relative path gives the location of a file or directory relative to your current working directory.
It does not start with /.
It’s like saying “my neighbor’s house” instead of giving the full address.

Key Points:

  • Based on your current directory
  • Does not start with /
  • Shorter, but it depends on where you are

Examples:

Suppose you are in /home/thor/:

  • Documents/file.txt → means /home/thor/Documents/file.txt
  • ../config/settings.conf → means “go up one directory, then into config, then open settings.conf”

Special symbols in relative paths:

. → current directory

.. → parent directory

Examples:

  • ./script.sh → run script.sh in the current directory
  • ../backup → go one level up and into backup
Navigating Paths in the File System

Files in Linux are case-sensitive, and you can refer to files by using their absolute path or their relative path!

The pwd command displays the full path name of the current working directory for that shell. This command helps you determine the syntax to reach files by using relative path names.

The ls command lists directory contents for the specified directory or, if no directory is given, for the current working directory.

Use the cd command to change your shell’s current working directory. If you do not specify any arguments to the command, then the shell changes to your home directory.

The following example changes the current working directory by using a mixture of absolute and relative paths with the cd command:

user@host:~$ pwd
/home/user
user@host:~$ cd Videos
user@host:~/Videos$ pwd
/home/user/Videos
user@host:~/Videos$ cd /home/user/Documents
user@host:~/Documents$ pwd
/home/user/Documents
user@host:~/Documents$ cd
user@host:~$ pwd
/home/user
user@host:~$

Curiosity:

The prompt displays the tilde character (~) when your current working directory is your home directory. When navigating within your home directory, the prompt displays the tilde (as a shortcut to your home directory) and the path to the current working directory. When your shell's current working directory is outside of your home directory, the prompt displays the absolute path to the current location.

The ls command has multiple options for displaying attributes on files. The most common options are -l (long listing format)-a (all files, including hidden files), and -R (recursive, to include the contents of all subdirectories). For example:

user@host:~$ ls -l
total 0
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Desktop
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Documents
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Downloads
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Music
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Pictures
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Public
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Templates
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Videos

user@host:~$ ls -la
total 40
drwx------. 17 user user 4096 Mar  2 03:07 .
drwxr-xr-x.  4 root root   35 Feb 10 10:48 ..
drwxr-xr-x.  4 user user   27 Mar  2 03:01 .ansible
-rw-------.  1 user user  444 Mar  2 04:32 .bash_history
-rw-r--r--.  1 user user   18 Aug  9  2021 .bash_logout
-rw-r--r--.  1 user user  141 Aug  9  2021 .bash_profile
-rw-r--r--.  1 user user  492 Aug  9  2021 .bashrc
drwxr-xr-x.  9 user user 4096 Mar  2 02:45 .cache
drwxr-xr-x.  9 user user 4096 Mar  2 04:32 .config
drwxr-xr-x.  2 user user    6 Mar  2 02:45 Desktop
drwxr-xr-x.  2 user user    6 Mar  2 02:45 Documents
...output omitted...

At the top of the listing are two special directories. One dot (.) refers to the current directory, and two dots (..) refer to the parent directory. These special directories exist in every directory on the system, and they are helpful when using file management commands.

Important:

File names that begin with a dot (.) indicate hidden files; they are not included in the regular output of ls and other commands. This behavior is not a security feature. Hidden files keep necessary user configuration files from cluttering home directories. Many commands process hidden files only with specific command-line options, and prevent one user's configuration from being accidentally copied to other directories or users.

Protecting file contents from improper access requires the use of file permissions!

The cd command has many options. Some options are helpful to practice early and to use often.

For example, the cd - command returns your shell prompt to the previous directory, the working directory you were using immediately before the current working directory. The following example illustrates this behavior and alternates between two directories, which is useful when processing a series of similar tasks:

user@host:~$ cd Videos
user@host:~/Videos$ pwd
/home/user/Videos
user@host:~/Videos$ cd /home/user/Documents
user@host:~/Documents$ pwd
/home/user/Documents
user@host:~/Documents$ cd -
user@host:~/Videos$ pwd
/home/user/Videos
user@host:~/Videos$ cd -
user@host:~/Documents$ pwd
/home/user/Documents
user@host:~/Documents$ cd -
user@host:~/Videos$ pwd
/home/user/Videos
user@host:~/Videos$ cd
user@host:~$

The cd .. command uses the hidden parent directory (..) to move the current working directory of your shell prompt up one level, without requiring the absolute path to the parent directory. The other hidden directory (.) specifies the current directory on commands where the current location is either the source or destination argument, and avoids the need to type the directory’s absolute path name:

user@host:~/Videos$ pwd
/home/user/Videos
user@host:~/Videos$ cd .
user@host:~/Videos$ pwd
/home/user/Videos
user@host:~/Videos$ cd ..
user@host:~$ pwd
/home/user
user@host:~$ cd ..
user@host:/home$ pwd
/home
user@host:/home$ cd ..
user@host:/$ pwd
/
user@host:/$ cd
user@host:~$ pwd
/home/user
user@host:~$

That’s it for now 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *