57

Where are filenames stored on a filesystem?

It's not in inode or with the actual file content since we have hard link that two filenames can point to the same inode.

Sildoreth
  • 1,884
yegle
  • 1,149

3 Answers3

73

I wasn't finding a suitable duplicate so here's an answer to your question.

File names & directories

excerpt

File names and directory implications:

  • inodes do not contain file names, only other file metadata.
  • Unix directories are lists of association structures, each of which contains one filename and one inode number.
  • The file system driver must search a directory looking for a particular filename and then convert the filename to the correct corresponding inode number.

Source: Wikipedia page on Inode

So the name of the file is stored within the directories' information structure. For example:

                         ss of fs

Directory's structure

excerpt

In the EXT2 file system, directories are special files that are used to create and hold access paths to the files in the file system. Figure 9.3 shows the layout of a directory entry in memory.

A directory file is a list of directory entries, each one containing the following information:

  • inode - The inode for this directory entry. This is an index into the array of inodes held in the Inode Table of the Block Group. In figure 9.3, the directory entry for the file called file has a reference to inode number i1,
  • name length - The length of this directory entry in bytes,
  • name - The name of this directory entry.

The first two entries for every directory are always the standard . and .. entries meaning "this directory" and "the parent directory" respectively.

Here's the Figure 9.3 references above:

                 ss #2

Source: The Linux Documentation Project: Filesystem

References

slm
  • 369,824
9

The file name is stored in the respective directory ("directory file"). This entry points to an inode.

Hauke Laging
  • 90,279
  • 1
    I can't see how symlink targets could end-up in directory entries. The symlink target may be found in the symlink's inode, but not in (any of) the directory entry that symlink is linked to, that would not make sense. – Stéphane Chazelas Mar 31 '14 at 20:57
  • 1
    @StéphaneChazelas, the symlink itself simply contains another name that should be opened instead. The name lookup starts again on the new name, which ( if it exists at all ) is a directory entry somewhere. – psusi Jun 03 '15 at 01:50
5

Filenames are stored in the directory data structures, which have the filename (a string) and the corresponding inode number.

directory is responsible for mapping filename --> inode.

and inode is responsible for mapping data area location --> sector on disk.

ilkkachu
  • 138,973