Read a directory to see the file to inode mapping

A correction: the directory is a list of fileNAME-inode pairs. And it's not an "ordinary" file. Like symlinks, sockets, and device nodes, its behaviour is different from that of ordinary files.

From the shell, you can see the mapping with ls -i.

From C, the structure returned by readdir() contains a d_name and a d_ino element, from which you can also see this mapping.

From userspace, the fact that a directory maps filenames to inodes is not usually all that important, because the kernel requires that you designate files by their name anyway. It doesn't let you ask for a file by inode number.

Symbolic links are another example of a type of file which contains information that can't be read as though it were a byte stream with systems calls like read(). Like an ordinary file, it contains data. In this case the data has special meaning: it's a pathname (which is a string) naming the target of the symlink. Unlike an ordinary file, the contents are not written using write() but with symlink(), and the contents are not read using read() but with readlink().


A directory is just like a file, but the kernel restricts access by only allowing certain system calls to open, read, and write to a directory entry. Here are some examples of the differences in C functions:

File      Directory
open()    opendir()
read()    readdir()
write()   N/A
close()   closedir()