Skip to content

File structure

The file structure in Linux refers to how files and directories are organized in the filesystem. This structure is hierarchical, starting from the root directory (/) and branching out to subdirectories and individual files. Here’s a comprehensive breakdown of the Linux file structure:

1. Overview of the Linux File Structure

Linux uses a Filesystem Hierarchy Standard (FHS) to define the directory structure and directory contents in Unix-like operating systems. The FHS ensures consistency across different Linux distributions, making it easier to manage and navigate the system. The file structure is designed to be a tree-like structure with the root directory (/) at the top, branching out to various subdirectories.

2. Key Directories in Linux File Structure

Here are the main directories found in a typical Linux file system:

2.1 / (Root Directory)

  • Description: The top-level directory of the Linux file system. All other directories are subdirectories of /.
  • Example: /bin, /etc, /home, /usr.

2.2 /bin (Essential Binaries)

  • Description: Contains essential command binaries that are needed for the system to boot and run in single-user mode. These are executable files that are necessary for basic system functionality.
  • Examples: ls, cp, mv, cat.

2.3 /boot

  • Description: Stores files required for booting the system, including the Linux kernel and initial RAM disk image.
  • Examples: vmlinuz (compressed kernel image), initrd.img (initial RAM disk).

2.4 /dev (Device Files)

  • Description: Contains device files that represent hardware devices. These files allow applications to interact with hardware devices.
  • Examples: /dev/sda (first hard disk), /dev/tty (terminal devices), /dev/null (a special file that discards all data written to it).

2.5 /etc (Configuration Files)

  • Description: Holds system-wide configuration files for the operating system and installed applications. It contains system settings and initialization files.
  • Examples: /etc/passwd (user account information), /etc/fstab (file system table), /etc/hosts (hostname mapping).

2.6 /home

  • Description: Contains user home directories. Each user has a subdirectory here, typically named after their username, where their personal files, configurations, and documents are stored.
  • Example: /home/user1, /home/user2.

2.7 /lib (Shared Libraries)

  • Description: Contains shared library files that are required by programs in /bin and /sbin to run. These libraries are essential for the execution of commands.
  • Examples: libc.so (standard C library), libm.so (math library).

2.8 /media

  • Description: Typically used for mounting removable media such as USB drives, DVDs, and CD-ROMs. This is where external devices are mounted when connected.
  • Example: /media/usb, /media/cdrom.

2.9 /mnt

  • Description: Used as a mount point for temporary file system mounts. It is usually used for mounting other file systems or partitions manually.
  • Example: /mnt/data, /mnt/backup.

2.10 /opt

  • Description: Contains optional software and applications that are not part of the core system. Software installed here is typically third-party and not managed by the package manager.
  • Example: /opt/google/chrome for Google Chrome.

2.11 /proc

  • Description: A virtual filesystem that provides information about system processes and kernel parameters. It is dynamically generated and does not consume disk space.
  • Examples: /proc/cpuinfo (CPU information), /proc/meminfo (memory usage), /proc/[PID] (information about a specific process).

2.12 /root

  • Description: The home directory for the root user (the system administrator). It is separate from /home to protect root’s personal files.
  • Example: /root/.bashrc, /root/.profile.

2.13 /run

  • Description: A temporary filesystem that contains data for the current boot session. It holds information about the system and running processes.
  • Example: /run/lock (lock files), /run/user (user-specific runtime files).

2.14 /sbin (System Binaries)

  • Description: Contains essential system administration binaries that are used for system maintenance and repair. These commands are typically run by the superuser.
  • Examples: fsck, shutdown, mount.

2.15 /srv

  • Description: Contains data for services provided by the system. This directory holds data that is served by the system, such as web server files.
  • Example: /srv/www for web content.

2.16 /sys

  • Description: A virtual filesystem that provides information and configuration options for the kernel and devices. It represents the system’s hardware as a directory tree.
  • Example: /sys/class/net (network interfaces), /sys/devices (device files).

2.17 /tmp

  • Description: Used for storing temporary files created by applications and processes. Files in this directory are often deleted upon system reboot.
  • Example: /tmp/session12345, /tmp/output.log.

2.18 /usr

  • Description: Contains user-related programs and data. It is split into several subdirectories:
    • /usr/bin: Most user commands.
    • /usr/lib: Libraries for /usr/bin programs.
    • /usr/local: Software installed locally by the system administrator.
    • /usr/share: Shared data for applications.
  • Examples: /usr/bin/python3, /usr/lib/libssl.so.

2.19 /var

  • Description: Contains variable data files, such as log files, spool files, and application cache. These files change in size and content frequently.
  • Examples: /var/log (log files), /var/spool (print jobs and mail queues), /var/tmp (temporary files that persist across reboots).

3. File Structure Hierarchy

The Linux file structure is hierarchical, forming a tree-like structure where each directory or file is connected by a path. The path starts from the root directory (/) and specifies the location of a file or directory. Paths can be absolute (starting from /) or relative (starting from the current working directory).

  • Absolute Path: /home/user1/documents/file.txt
  • Relative Path: documents/file.txt (assuming the current working directory is /home/user1)

4. File Structure Permissions and Ownership

Each file or directory in Linux has associated permissions and ownership, which control access to the files:

  • Permissions: Indicate who can read, write, or execute the file.
  • Ownership: Consists of the user (owner) and group associated with the file.

Example of permissions:

-rw-r–r– 1 user1 group1 4096 Dec 2 12:34 myfile.txt

  • -rw-r–r– indicates read and write permission for the owner, and read-only permission for the group and others.

5. Navigating the File Structure

Common commands to navigate and interact with the file structure include:

  • cd (Change directory): Used to navigate between directories.
  • ls (List): Lists files and directories.
  • pwd (Print working directory): Displays the current directory path.
  • mkdir (Make directory): Creates a new directory.
  • rmdir (Remove directory): Deletes an empty directory.
  • rm (Remove): Deletes files or directories.

Conclusion

Understanding the Linux file structure is essential for effectively managing the system, troubleshooting issues, and performing administrative tasks. The hierarchical nature of the file system, combined with various key directories and their purposes, makes Linux powerful and flexible for users and administrators.