$_ CLI In Depth
Learning progress 0%
lesson filesystem inodes navigation

Inodes & Directory Entries

6 min read Module 2: Filesystem Navigation & Inodes Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:14

A name is not the file

Most people think of a file as a thing with a name. The kernel thinks of it differently. The data and the metadata about it live in an object called an inode, which has a number that is unique on its filesystem. A directory is a table that maps names to inode numbers. When you open notes.txt, the kernel looks up that name in the directory, finds an inode number, and then works with the inode from that point on. The name is the handle you use to reach the inode, not the inode itself.

This is the reason the same data can have several names, and the reason you can change a name without copying anything. A rename inside one directory updates one entry in the table. It does not touch the data blocks on disk.

What an inode stores

An inode holds the file type and permission bits, the owner user and group IDs, the size in bytes, a link count, and three timestamps. It also records the locations of the data blocks, either directly or through indirect structures, depending on the filesystem. The name of the file is not stored in the inode. That is why stat can show you everything about a file except its name, which it gets from the path you typed.

Each inode also has a number, and ls -i prints it in front of each entry. The numbers are meaningful only on one filesystem. Two files on different disks can have the same inode number, and the number is not a stable identifier across machines or backups.

BASH4 lines
cd /workspace
echo hello > note.txt
ls -li note.txt
stat -c '%i %h %s %n' note.txt

The first column of ls -li is the inode number. The stat format string prints the inode number, the hard link count, the size, and the name. The two numbers match because they describe the same inode.

The directory is a table of names

A directory is a file whose contents are a list of records. Each record holds an inode number and a name, and the filesystem stores the records in a structure that makes lookup fast. When a program lists a directory, the kernel reads those records and returns them. The program then calls stat on each name if it wants the metadata. This is why listing a directory of many files with long format is slower than listing names alone. The names are cheap, and the metadata requires one lookup per entry.

Creating a file means adding a record to a directory and allocating an inode. Deleting a file, in the common case, means removing the record from the directory. The inode and its data blocks are released only when no name refers to them and no process still has the file open.

Link counts and deletion

The link count in the inode counts how many directory entries point at it. A new regular file starts with a count of one. Adding a hard link increases the count by one, and removing any name decreases it. The kernel frees the data only when the count reaches zero and no process holds the file open. This second rule is the one that surprises people. If a program is writing to a log file and you delete the log, the name disappears at once, but the space is not freed until the program closes the file. The program keeps writing to an inode that no longer has a name.

BASH5 lines
cd /workspace
exec 3> scratch.txt
rm scratch.txt
ls -l /proc/$$/fd/3
exec 3>&-

The listing of the descriptor shows a target such as /workspace/scratch.txt (deleted). The data still exists through that file descriptor. Closing descriptor 3 finally releases it.

From a name to an inode, step by step

When you open a path such as /workspace/notes/todo.txt, the kernel starts at the root inode and reads the directory /, which is a table of names. It finds the entry for workspace, takes that inode number, and reads that directory. It repeats the lookup for notes and finally for todo.txt. Each step reads one table and produces one inode number. The kernel caches the results in a structure called the dentry cache, so repeated lookups of the same path are fast, but the logic is the same.

This model explains why a rename of a parent directory does not change the inode numbers of the files inside it. The names along the path change, but the inodes do not move. It also explains why a file can be opened after its directory has been removed, if the process already holds a descriptor for it, and why a path lookup can fail halfway through when a directory on the path is missing or is not searchable by the current user.

Inodes can run out

Filesystems create a fixed number of inodes when they are made, and the count is separate from the number of data blocks. A disk can have free space and still refuse to create files if every inode is in use. This happens with filesystems that hold millions of tiny files. The command df -i reports inode usage on systems that support it. Running out of inodes produces the error "No space left on device", which is confusing if you only check the size of the disk.

Note

Inode numbers are assigned by the filesystem. Do not use them as a permanent identifier for files in scripts. If you need to know that two paths name the same file, compare the device and inode numbers that stat reports for both, in the same command.

Practice in the lab

The lab asks you to see an inode number for a file and then to create a second name for the same inode. Keep in mind that the number itself will differ between machines and between runs. The check looks at what the two names share, not at the value of the number.

Visualizer

A directory is a list of (name, inode number) pairs. The inode holds the file itself. Hard links add names; symlinks store a path.

1 / 5
directory /workspace
nameinode
a1201
inode table
inodetypelinksdata
1201regular1"hi\n"

echo hi > a allocates inode 1201 and adds the entry a → 1201 to the directory. The link count is 1.

cd /workspace; echo hi > a; ln a b; ls -li a b; rm a; cat b; stat -c "%h links" b
Terminal lab · 2 steps · real Linux in your browser

See an inode and a second name

Read an inode number, then create a hard link and confirm that both names share it.

01
Read the inode number
Print the inode number of the file, together with its name: ``bash ls -i inodes/original.txt > inode.txt ``
Show hint for step 1
ls -i puts the inode number in front of each entry.
02
Create a hard link
Create a second name for the same inode: ``bash ln inodes/original.txt inodes/alias.txt `` Then run ls -li inodes to see that both names share one number.
Show hint for step 2
ln without -s creates a hard link. Both names must show the same inode number.
Check yourself Question 1 of 4
Concept Check What a directory entry holds recall

What does one entry in a directory contain?

Recap & Key Takeaways

A file name is an entry in a directory that points at an inode; the inode holds metadata and data locations.
A directory is a table that maps names to inode numbers, and renaming updates one entry.
The kernel frees the data only when the link count is zero and no process has the file open.
Filesystems have a fixed number of inodes, so a disk can run out of them before it runs out of space.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue