Search code examples
unixfilesystemsinode

Are the period commands "." and ".." actually referencing a file?


Since everything in Unix is a file, when we call "cd ." are we actually cding into the directory . ? Is it a protected symbolic reference to the parent directory of each directory?


Solution

  • Yes, everything in Unix, is a file. Like any directory, a file of any type, any device(speaker, keyboard,. etc.) and even a file-system itself, all act like a file for OS. In Unix every file has a inode attached with it, which contains the file metadata like info about permissions, size, time-stamps and most importantly file data block pointers which point to data block containing actual file data.

    Hence each Directory(being a file) also has a inode. The content of the directory is the sequence of records. Each record has at least two fields which are filename and the inode number.

    file1name  file1_inode_number
    

    Exact structure of record depends on the filesystem implementation. So basically directory file contain a (record)entry corresponding to each file and immediate sub-directory inside it. In addition to that, Directory file also contain 2 more entries which are

    .    :  mapped with self inode 
    and 
    ..   :  mapped with parent's inode
    

    so all over directory structure looks like

    .          inode_number_of_self
    ..         inode_number_of_parent_dir
    file1name  inode_number_of_file1
    file2name  inode_number_of_file2
    .
    .
    so on
    

    So whenever you cd ./ or cd ../ OS is referring current or parent directory(respectively) relative to your current directory.