Just started reading a bit about the linux file system. In several places I found quotes like this one:
Unix directories are lists of association structures, each of which contains one filename and one inode number.
So I expected to find out that each directory would contain the names of the files under it, with each file mapped to an inode. But when I do vim directory_name
in ubuntu, I get something like this:
" ============================================================================
" Netrw Directory Listing (netrw v156)
" /Users/user/workspace/folder
" Sorted by name
" Sort sequence: [\/]$,\<core\%(\.\d\+\)\=\>,\.h$,\.c$,\.cpp$,\~\=\*$,*,\.o$,\.obj$,\.info$,\.swp$,\.bak$,\~$
" Quick Help: <F1>:help -:go up dir D:delete R:rename s:sort-by x:special
" ==============================================================================
../
./
folder1/
folder2/
file1
file2
I expected to see an inode number next to each file name, why isn't this the case?
Best Answer
A directory is, semantically speaking, a mapping from file name to inode. This is how the directory tree abstraction is designed, corresponding to the interface between applications and filesystems. Applications can designate files by name and enumerate the list of files in a directory, and each file has a unique designator which is called an “inode”.
How this semantics is implemented depends on the filesystem type. It's up to each filesystem how the directory is encoded. In most Unix filesystems, a directory is a mapping from filenames to inode numbers, and there's a separate table mapping inode numbers to inode data. (The inode data contains file metadata such as permissions and timestamps, the location of file contents, etc.) The mapping can be a list, a hash table, a tree...
You can't see this mapping with Vim. Vim doesn't show the storage area that represents the directory. Linux, like many other modern Unix systems, doesn't allow applications to see the directory representation directly. Directories act like ordinary files when it comes to their directory entry and to their metadata, but not when it comes to their content. Applications read from ordinary file with system calls such as
open
,read
,write
,close
; for directories there are other system calls:opendir
,readdir
,closedir
, and modifying a directory is done by creating, moving and deleting files. An application likecat
usesopen
,read
,close
to read a file's content; an application likels
usesopendir
,readdir
,closedir
to read a directory's content. Vim normally works likecat
to read a file's content, but if you ask it to open a directory, it works likels
and prints the data in a nicely-formatted way.If you want to see what a directory looks like under the hood, you can use a tool such as
debugfs
for ext2/ext3/ext4. Make sure you don't modify anything! A tool likedebugfs
bypasses the filesystem and can destroy it utterly. The ext2/ext3/ext4debugfs
is safe because it's in read-only mode unless you explicitly allow writing through a command line option.You'll see the names of the directory entries in
/
amidst a bunch of other characters, some unprintable. To make sense of it, you'd need to know the details of the filesystem format.