Linux Directory Structure

Sakeena ShaikSakeena Shaik
7 min read

What is Linux Directory Structure?

Linux keeps all the devices and data in the form of files in different locations.
It contains a root directory ->sub directories -> Files

simply, we can say that as a hierarchical layout of directories and files which allows us to understand how the data can be organized, stored, and accessed providing a standardized method for the OS to locate and manage files. This structure starts from a single root directory, denoted by /, and branches into various directories, each serving specific functions and purposes.

Why it is important?
To understand clearly think of the Linux directory structure as a well-organized library πŸ“š
πŸ“‚Efficiency: Imagine walking into a library where books are scattered everywhere. Finding a specific book would be a nightmare! Similarly, the Linux directory structure organizes files and directories systematically making it easy to locate and manage them.
βœ…Consistency: Like every library file has a cataloging system, Linux follows a standard directory structure. This consistency means that no matter which Linux distribution you use the basic layout remains the same, it’s like knowing exactly where the fiction section is in any library.
πŸ”Security: In our library analogy, valuable books are kept in a special section with restricted access. Similarly, Linux uses directory permissions to control who can access and modify files, ensuring security and stability.
🎯Modularity: The structure allows different parts of the system to work together smoothly, much like different sections of a library complement each other, it all serves unique, yet interconnected purposes.
πŸ”„Functionality: Each directory has a specific role, like /bin for essential tools, /etc for configuration files, /home for user directories, and /var for logs.
We will deep dive into each concept in later sections.

Linux directory structure:

by maintaining this neat and intuitive structure, Linux ensures that users and applications can navigate the system effortlessly.
It can be viewed in the ubuntu terminal as follows:

The –L option with tree utility can be used to display how many levels down you want to see

Navigating the Linux directory structure is crucial for anyone working with Linux, Let's look at the key directories and what they contain:
πŸ—‚οΈ/ (root) directory:
The root directory is the top-level directory in the Linux file system hierarchy. Everything starts here and all the other files and directories are contained within this directory.
Eg: To view the list of contents of the root directory, use:

πŸ“‚/bin directory:

This directory holds the essential user command binaries (executable) that all users can access which are required for efficient system operation and repairs.
E.g.: Basic commands like β€˜ls’, β€˜cp’, β€˜mv’, β€˜cat’ etc.

πŸ‘¨πŸΏβ€πŸ’»/boot directory:
This is the location where you will find all the necessary files required for the system’s booting process including the Linux kernel executables and initial RAM disk images.
To view the boot files, use:
# ls /boot

πŸ—ƒοΈ/dev directory:

This directory is home to all device files such as hard disks, USB, CD-ROM, terminal devices, etc. This file allows the system to interact with hardware components. Device files in /dev allow software to treat hardware devices like files, enabling standard file operations (read, write, etc.) on devices.

Eg: To view hard disk partitions in your current system use below:

βš™οΈ/etc directory: This directory contains all the system-wide configuration files and various scripts that are used to manage the system settings. It is loaded with configuration files related to specific applications, network mappings, authentication data, and various system start-up and shutdown scripts.
Example:

/etc/passwd: contains user account information
/etc/fstab: contains info about disk drives and partitions
/etc/hosts: Maps host names to IP addresses
/etc/ssh: contains configuration files for the SSH service

To view the content of any file can make use of cat command:

🏑/home directory:

-> The home directory is where personal directories for users are located, each user will have a sub-directory within /home.
->It contains user-specific files and configurations.
->Each user’s home directory is isolated from one another providing a personal workspace.

To list user home directories:

Changing to a user’s home directory:

πŸ“š/lib directory:
It contains shared library files which are essential for the system's basic functionalities. It also contains kernel modules that are loaded into the kernel during boot or runtime.

Example:
/lib/modules: contains kernel modules
/lib/systemd: contains systemd system and service manager files
/lib/x86_64-linux-gnu: contains libraries for the x86_64 architecture

πŸ“Έ/media directory:
This directory is used for mounting all removable media such as CDs, DVDs, USB devices, etc. Whenever you insert any external storage it will be automatically mounted in this directory.
Example:
/media/usb: mount point for a USB drive
/media/cdrom: mount point for a CD-ROM

πŸ› οΈ/mnt directory: This directory serves as a location where system administrators mount file systems for various administrative tasks and is used for temporarily mounting the file systems. Used for both manual mounting and unmounting file systems. By default, it will be empty.

πŸ’Ύ/opt directory:
/opt directory is used for installing optional or third-party software packages that are not part of the default system installation.
Example:
/opt/VMware: may contain VMware related software
/opt/google: may contain Google software like chrome

🌲/root directory:
It is the home directory for the root user. It is different from β€˜/’, which is the root of the entire file system hierarchy.

πŸ”’/sbin directory:
contains the essential binaries used by the system administrators for system maintenance and troubleshooting. Commands in this directory are therefore generally used by the root user.
Example:
/sbin/init: responsible for starting the system
/sbin/ifconfig: Used to configure network interfaces

πŸ“‰/srv directory:
It holds the data related to the services provided by the system such as web and FTP servers.
It stores the service-related data.

πŸ”§/sys directory:
The /sys directory is a virtual file that provides information about kernel, hardware devices and associated device drivers. It is used for querying and modifying kernel parameters at run-time.
Example:
/sys/class: info about device classes
/sys/devices: info about devices

πŸ—‚οΈ/tmp directory:
Used to store temporary files which will be cleared upon system reboot. It provides a space for temporary storage.

πŸš€/run directory:

stores run-time data for processes and system services.
Contains data that is relevant only during the current boot session
Helps to avoid the need to create temporary files in other locations
Example:
/run/shm: contains shared memory
/run/usr: contains the user’s run-time data

πŸ”Ž/proc directory:

The /proc directory is a virtual file system, also known as a process information pseudo-filesystem. It doesn't store real files but provides runtime system information like memory, mounted devices, and hardware configuration. It serves as a control and information center for the kernel. Many system utilities, such as lsmod (equivalent to cat /proc/modules) and lspci (equivalent to cat /proc/pci), access files in this directory. Additionally, you can read and modify kernel parameters in real-time by altering files in /proc.

For eg: To check memory/cpu info, use below:
cat /proc/meminfo
cat /proc/cpuinfo

πŸ“Š/var directory:

The /var directory contains files that are expected to grow in size, such as logs, spool files, and temporary email files.
Example:
/var/log: system log files
/var/spool: spool directories for tasks like printing
/var/tmp: Temporary files that persist across reboots

to list variable files:

To view log files:

πŸ‘¨πŸ»β€πŸ’»/usr directory:
It contains user utilities and applications, as well as shared resources used by these programs. The /usr directory is typically subdivided into several key directories, each serving a specific purpose.
Some of the key directories are as follows:
1) /usr/bin: This directory contains most of the user binaries for standard utilities & applications
Eg: β€˜cp’, β€˜grep’, β€˜find’ etc.

Listing user binaries:

To run a program:

2)/usr/share: The /usr/share directory contains architecture-independent data files shared by all users, such as documentation, configuration files, and other shared resources.

To view manual pages for installed commands and utilities:

3)/usr/lib:
it contains shared libraries and modules needed by the binaries in /usr/bin and other directories.
Listing shared libraries:

To view kernel modules:

4)/usr/local:
used for locally installed software and custom scripts that are not managed by the system's package manager. It mirrors the structure of /usr but is intended for software installed from source or other third-party sources.
Example:
/usr/local/bin: locally installed user binaries
/usr/local/lib: locally installed libraries

Whether you're a beginner or an experienced user, familiarizing yourself with the Linux directory hierarchy empowers you to navigate the system more efficiently, perform administrative tasks with confidence, and troubleshoot issues effectively. Mastery of the directory structure is an essential skill for anyone looking to leverage the full potential of Linux.

Explore, experiment, and make the most of your Linux journey. Happy navigating!

0
Subscribe to my newsletter

Read articles from Sakeena Shaik directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sakeena Shaik
Sakeena Shaik

🌟 DevOps Specialist | CICD | Automation Enthusiast 🌟 I'm a passionate DevOps engineer who deeply loves automating processes and streamlining workflows. My toolkit includes industry-leading technologies such as Ansible, Docker, Kubernetes, and Argo-CD. I specialize in Continuous Integration and Continuous Deployment (CICD), ensuring smooth and efficient releases. With a strong foundation in Linux and GIT, I bring stability and scalability to every project I work on. I excel at integrating quality assurance tools like SonarQube and deploying using various technology stacks. I can handle basic Helm operations to manage configurations and deployments with confidence. I thrive in collaborative environments, where I can bring teams together to deliver robust solutions. Let's build something great together! ✨