Search code examples
unixdirectoryfilesystemsinode

How the UNIX file system forms a structure with nodes


I'm trying to understand how the UNIX file system works with regards to inodes. As I understand, a directory is represented as simply a table with an entry of the form [name:inode] for every subdirectory or file.

Where I am confused is how these directories form a structure. If i had a directory 'root' containing a subdirectory 'home' which contains a file 'file.txt' then i believe the root directory would have an entry ['home':(homes associated inode)] but then how does this link to the 'home' directory table?

Any help or guidance would be much appreciated, Ben


Solution

  • Your understanding is mostly correct. Here's a picture that might help. I'm going to assume that the root directory is known to be inode 1. So we might have:

    inode 1:

    type: directory
    contents:
        usr       17
        etc       49
        home      57
    

    inode 57:

    type: directory
    contents:
        dmr       201
        scs       857
        ben       981
    

    inode 981:

    type: directory
    contents:
        .bashrc   1045
        projects  1191
        file.txt  2043
    

    inode 2043:

    type: file
    contents:
        This is
        my text file.
    

    Here I've shown the situation that the full path (or at least, a full path) of your text file is /home/ben/file.txt.

    (I've also made one big simplification. Typically, a file's contents are not stored in the inode, but rather, in other disk blocks, with the inode containing pointers to those other blocks.)