ruk·si

🐧 Linux
Directory Hierarchy

Updated at 2019-11-21 18:01

This note is about basic directory hierarchy in Unix-like operating systems. You should follow these guidelines on computers that are shared with multiple users. But as the filesystem hierarchy enforcing is lax, hierarchies on personal computers is usually not even closely following these guidelines though.

Everything in Unix system is treated as files. There are three types of files: ordinary files, directory files and device files.

Summarized directory structure:

/
# The slash character alone denotes the root of the filesystem.

/bin
# Binaries
# Commonly accessible executable commands for all admins, users and scripts.
# Should only contain the minimal programs to keep the whole system running.
# Should not contains subdirectories.
# Examples: cat, cd, kill, mv, rm.

/boot
# Boot
# Contains all the important files which are required to boot up the computer.
# Examples: bootloader, image of the kernel.

/dev
# Devices
# Contains file representations of devices and pseudo-devices.

    /dev/null
    # Null Device
    # All data written to this pseudo-device is discarded.

/etc
# Essential Text Configuration
# Contains system configuration files.
# Should not contain executable binaries.
# Files should be static.
# Examples: networking settings, graphical X server configuration.

/home
# Home
# Contains directories for each user e.g. /home/ruksi or /home/tommi.
# Fairly standard concept but the setup differs e.g. /Users/<NAME> on OS X.
# No program should rely on /home existing.

/lib
# Libraries
# Contains libraries for important programs in /bin and /sbin.

    /lib/bin
    # Library Binaries
    # Should only contain the minimal programs to keep the whole system running.

/lost+found
# Lost And Found
# Contains orphaned files, might be handy sometimes.

/media
# Media
# Default mount point for removable media.
# Examples: USB drives, CD drives and Floppy drives.

/mnt
# Mount
# Contains mount points for temporary filesystems.
# Setup may differ e.g. /Volumes on OS X.
# Example: multiple hard disks.

/opt
# Optional
# Contains add-on software.
# Some large program suites may prefer to be installed here rather than /usr.
# Also prefer to install any software in here that should be easily removable.
# Install programs in subdirectories: /opt/<PACKAGE> or /opt/<PROVIDER>.

/proc
# Process
# Virtual filesystem showing information about processes as files.
# Avoid using manually.

/root
# The home directory for the superuser "root", the system administrator.

/sbin
# System Binary
# Contains fundamental utilities needed to manage the low level systems.
# Should only contain the minimal programs to keep the whole system running.
# Examples: init, mkfs, fsck, reboot.

/srv
# Server related data files used by the services of the system.
# May be sorted by /srv/<PROTOCOL> or /srv/<GENRE>/<PROTOCOL>.
# Examples by protocol: /srv/www, /srv/ftp, /srv/csv.
# Examples by genre: /srv/physics/, /srv/compsci/ftp, /srv/math/www.

/sys
# System
# Contains information related to the hardware.

/tmp
# Temporary
# Files that can be removed from the system upon startup.

/usr
# Unix System Resources (commonly called "user")
# Contains more computer-centric executables, libraries and shared resources.
# Follows the same directory structure as /.

    /usr/bin
    # Unix System Resource Binaries
    # Contains user-centric binaries.
    # Examples: python, perl

    /usr/include
    # Unix System Resource Include
    # Stores the C headers you use on your development.

    /usr/lib
    # Unix System Resource Library
    # Contains libraries and data files for programs in /usr.

    /usr/local
    # Local Unix System Resources
    # Once again resembles /.

        /usr/local/bin
        # Local Unix System Resource Binaries
        # Used to install permanent programs, usually compiled by hand.
        # This is the best place to install new programs.

    /usr/share
    # Shared Unix System Resources
    # Shared architecture-independent data.

/var
# Variable
# Contains files for system state information that may change often, but
# will not be removed on boot.

    /var/cache
    # Cache data for applications.

    /var/lib
    # Persistent data modified by programs as they run.
    # Examples: databases, packaging system metadata.

    /var/lock
    # Lock files
    # Files keeping track of resources currently in use.

    /var/log
    # Logs
    # Contains system log files.

    /var/log/syslog
    # The lowest level system log file
    # Important for operating system debugging.

    /var/mail
    # Mails
    # Contains incoming mails.

    /var/run
    # Run-time
    # Contains run-time data about the system.
    # Removed at boot.
    # Examples: /var/run/crond.pid, /var/run/<PROGRAM>/

    /var/spool
    # Simultaneous Peripheral Operations On-Line
    # Contains print jobs, mail spools and other queued tasks.

    /var/tmp
    # Temporary
    # Contains temporary files that should be preserved between
    # system reboots, but are still removed periodically.

Where should I store data relevant to my application?

  • When in doubt, just use /tmp.
  • Use /var/tmp for large data that may not fit in RAM as /tmp can sometimes be memory backed.
  • Use /var/tmp for data that should be kept across reboots like longer term cache.
  • Use /dev/shm for long lived medium sized files with changing content that multiple processed need. These will be in shared memory.
  • Consider allowing user to configure the application data directories.

Sources