MTI TEK
  • Home
  • About
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All Resources
Linux-Ubuntu | Directory Structure (Filesystem Hierarchy)
  1. Directory Structure (Filesystem Hierarchy)
  2. The Root Directory
  3. Directory Paths
  4. Paths starting with a period (.), two periods (..), or a tilde (~)
  5. Using wildcard characters to identify files and directories names (metacharacter wildcards)

  1. Directory Structure (Filesystem Hierarchy)
    The filesystem stores information in a hierarchical structure that has the form of a tree.

    The root level of the tree is called the root directory, or virtual directory (represented by a single slash "/").
    The root directory contains all the files and directories from all the devices (physical or mount points) of the system.

    Each directory can have files and/or other sub-directories within it.

    A file or a directory that has its name beginning with the dot character (".") is by default hidden and is not shown by default in the file manager (it will also be hidden if you use the ls command without the option "-a").
  2. The Root Directory
    $ tree -L 1 /
    
    /
    ├── bin          # Essential user commands (cp, rm, ls, cat, ...) - available to all users
    ├── boot         # Bootable Linux kernel, boot loader config files (grub.cfg), initial ramdisk images
    ├── cdrom        # Traditional mount point for CD-ROM drives (legacy, now typically under /media)
    ├── dev          # Device files representing access points to hardware (/dev/sda1, /dev/tty, /dev/null, ...)
    ├── etc          # System-wide configuration files (passwd, fstab, hosts, crontab, ...)
    ├── home         # Individual user home directories (/home/username) - where users store personal files
    ├── lib          # Essential shared library files needed by programs in /bin and /sbin
    ├── lib64        # 64-bit shared library files for 64-bit programs
    ├── lost+found   # Recovery directory for orphaned files found during filesystem check
    ├── media        # Mount point for removable media devices (USB drives, CD-ROMs) - automatically mounted
    ├── mnt          # Mount point for temporarily mounting filesystems, network drives, disk partitions
    ├── opt          # Optional software packages and third-party applications
    ├── proc         # Virtual filesystem containing system and process information (/proc/cpuinfo, /proc/meminfo, ...)
    ├── root         # Root user's home directory - only accessible by root user (not regular users)
    ├── run          # Runtime data for applications and system processes (PID files, sockets, ...)
    ├── sbin         # System administrative commands (mount, fdisk, ...) - typically for root user
    ├── sys          # Virtual filesystem for kernel and hardware information
    ├── tmp          # Temporary files created by applications - typically cleared on reboot
    ├── usr          # User programs, libraries, documentation, and shared files (secondary hierarchy)
    └── var          # Variable data files (logs, databases, web content, mail spools, ...)
    
  3. Directory Paths
    You can identify a directory or a file by using its absolute path (starts from the root directory).

    You can also use a relative path (relative to the current directory) to identify a directory or a file.

    There are also some special forms to define the path of a directory or a file, by using the characters period or tilde (see below).

    The path can also contain variables that can be substituted by their values when resolving the path name (for example: $HOME, $PWD).

    Path Types Visual Reference:
    Absolute Path:  /home/user/documents/file.txt    (starts from root /)
    Relative Path:  documents/file.txt               (relative to current directory)
    Home Path:      ~/documents/file.txt             (starts from user's home)
    Parent Path:    ../documents/file.txt            (relative to parent directory)
    Current Path:   ./documents/file.txt             (relative to current directory)
    Variable Path:  $HOME/documents/file.txt         (using environment variable)
    
  4. Paths starting with a period (.), two periods (..), or a tilde (~)
    This directory structure will be used in the examples below:
    #|+ ${user.home}
    #   |+ folder
    #      |+ folder1
    #      |+ folder2
    $ mkdir ~/folder ~/folder/folder1 ~/folder/folder2
    
    • A path starting with a period (.) represents the absolute pathname of the current directory.
      $ cd ${user.home}/folder/
      $ ls -1 ./
      folder1
      folder2
      
    • A path starting with two periods (..) represents the absolute pathname of the parent of the current directory.
      $ cd ${user.home}/folder/folder1/
      $ ls -1 ../
      folder1
      folder2
      
    • A path starting with a tilde (~) represents the absolute pathname of the user home directory.
      $ ls -1 ~/folder
      folder1
      folder2
      
    • A string preceded by a tilde (~) represents the absolute pathname of the user home directory referred to by the value of the string.
      $ ls -1 ~mtitek/folder
      folder1
      folder2
      
    Common Environment Variables for Paths:
    • $HOME: represents the current user's home directory (same as ~)
      $ echo $HOME
      /home/username
      $ ls $HOME/Documents
      # equivalent to: ls ~/Documents
      
    • $PWD: represents the current working directory (same as .)
      $ echo $PWD
      /home/username/projects
      $ ls $PWD/src
      # equivalent to: ls ./src
      
    • $OLDPWD: represents the previous working directory
      $ cd /home/username/projects
      $ cd /tmp
      $ echo $OLDPWD
      /home/username/projects
      $ cd $OLDPWD  # returns to previous directory
      
    • $PATH: contains directories where executable files are located
      $ echo $PATH
      /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
      
  5. Using wildcard characters to identify files and directories names (metacharacter wildcards)
    You can use some wildcard characters (metacharacter wildcards) to match part of a file or a directory name.

    Basic Wildcards:
    • *: match any number of characters (including zero).
      $ cd ${user.home}/folder/
      $ ls fold* # all files starting with "test"
      folder1:
      folder2:
      
      $ ls *.txt        # all files ending with .txt   
      $ ls *fold*       # all files containing "fold" anywhere in name
      
    • ?: match exactly one character.
      $ cd ${user.home}/folder/
      $ ls f??der?
      folder1:
      folder2:
      
    • [...]: match one character from a set or range.
      $ cd ${user.home}/folder/
      $ ls f[a-z]*[0-9]
      folder1:
      folder2:
      
      $ ls file[123].txt       # matches file1.txt, file2.txt, file3.txt
      $ ls [A-Z]+.doc          # matches files starting with uppercase letters
      
    Advanced Wildcards:
    • [!...] or [^...]: match one character NOT in the set.
      $ ls file[!0-9].txt      # matches files like filea.txt, fileb.txt (not file1.txt)
      $ ls [!.]*.txt           # matches files not starting with a dot
      $ ls *[!~]               # matches files not ending with ~
      
    • **: recursive wildcard (with find command or in some shells).
      $ find . -name "*.txt"          # finds all .txt files recursively
      $ find . -name "**/config.xml"  # finds config.xml in any subdirectory
      
    Brace Expansion:
    You can use curly braces ({}) to expand a set of characters or strings.
    $ touch file{1,2,3}.txt
    $ ls file*.txt
    file1.txt  file2.txt  file3.txt
    
    $ mkdir -p src/{main,test}/{java,resources}
    $ tree src/
    src/
    ├── main
    │   ├── java
    │   └── resources
    └── test
        ├── java
        └── resources
    
© 2025 mtitek