I'm just learning about filesystems and it is said that a directory is just an ordinary file which keeps a list of file-inode pairs.

If I try to open the directory Downloads with any of the following cat less tail, it just outputs that its a directory and can't be opened.

Ideas how to read it?


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().

  • 1
    So there isn't a way to read it through the terminal directly with a command without writing a C program? I would like to see if there is anything else written besides the mappings. – TheMeaningfulEngineer Jan 16 '13 at 18:53
  • 2
    All of the tools like cat for reading regular files use read() and similar system calls that assume the file is a stream of bytes. But a directory is not (at least conceptually) as stream of bytes, it's a mapping. The kernel provides systems calls specifically for the purpose of reading this mapping. Tools like ls use these system calls. So short answer, yes: to read the contents of a directory, you need a tool designed to read directories. – Celada Jan 16 '13 at 18:56
  • If you are curious on the contents of a directory, it is just as is being told here: Filenames and corresponding inode numbers. Plus junk like space where the names of deleted files used to be, space at the end of the filename that use to be called supercalifragilisticexpialidocious.c and is now called just bob.c, free space to round up to blocksize, ... Nothing to feed your favorite conspiracy theory, in fact. Directories used to be readable as regular files in simpler days, but as the structure became more complicated that is now forbidden. – vonbrand Jan 21 '13 at 19:47

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()

It also depends on the OS. I don't know the specific explanation but have experienced a difference between Linux and Unix behaviors in this regard. Specifically FreeBSD, you CAN cat a directory. Threw me off the first time I tried.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.