Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Files, inodes and directories

After this lesson: I can explain how a path is turned into a file.

55 min lesson8 commandsEveryday Linux commands lab: the thirty commands engineers type from memory

How a path leads to a file

A file system turns a path such as /home/student/notes.txt into the disk blocks that hold the file. It uses directories and records called inodes. Knowing these steps explains links, why renaming a huge file is instant and why a disk can run out of room with space still free.

This lesson follows one path, a step at a time.

By the end of this lesson

Note

I can explain how a path is turned into a file.

Key words

WordWhat it meansExample
Inodethe record that holds a file's details and where its blocks are
Directorya file that lists names and the inode for each
Blocka fixed-size piece of disk4096 bytes
Paththe list of names that leads to a file/home/student/notes.txt
Hard linka second name for the same inode
Symbolic linka small file that holds the path of another

The cells students fill in are left blank in this preview.

An inode holds details, not the name

A disk is a long row of blocks. For each file the system keeps one record, the inode. It holds the type, the owner, the permissions, the size, the times, the number of names and the list of blocks that hold the data.

The inode has a number, unique on that disk. It does not hold the file's name.

Print the type of a file, a folder

Command
stat -c %F /etc/passwd /

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Worked example: one inode

FieldExampleMeaning
Inode number14The record's number on this disk
Typeregular fileA file, a directory or a link
Owner and modestudent, rw-r--r--Who may read, write or run it
Size6 bytesHow much data it holds
Link count1How many names point at it
Block listblock 880Where the data sits on the disk

The cells students fill in are left blank in this preview.

A directory lists names and inodes

A directory is a file whose data is a list of pairs: a name and an inode number. That list is where names live.

To open /home/student/notes.txt, the system starts at the root directory, whose inode number is always known. It looks up home and gets an inode. That is a directory too, so it reads it and looks up student, and so on to the last name.

Worked example: resolving /home/student/notes.txt

StepDirectory readName looked upInode found
1root (inode 2)home5
2inode 5student9
3inode 9notes.txt14
Resultinode 14: the file

The cells students fill in are left blank in this preview.

Two names can point at one inode

Because a name is only a directory entry, a second entry can point at the same inode. That is a hard link. The inode counts its names, and the data goes only when the count reaches 0.

A symbolic link is different. It is a small file of its own that holds a path. Make three small files in the scratch folder, which the lab says is safe to delete from.

Make a file of six bytes

Command
printf 'hello\n' > ~/scratch/demo.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Print its size and its number of names

Command
stat -c '%s %h' ~/scratch/demo.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Give it a second name

Command
ln ~/scratch/demo.txt ~/scratch/demo-link.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Print the number of names again

Command
stat -c %h ~/scratch/demo.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Check that both names have one inode number

Command
[ "$(stat -c %i ~/scratch/demo.txt)" = "$(stat -c %i ~/scratch/demo-link.txt)" ] && echo same

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Make a symbolic link, print its type

Command
ln -s ~/scratch/demo.txt ~/scratch/demo-sym.txt && stat -c %F ~/scratch/demo-sym.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Remove the three files

Command
rm ~/scratch/demo.txt ~/scratch/demo-link.txt ~/scratch/demo-sym.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think a file's name is part of the file. The name lives in a directory, and the data lives under the inode. That is why a rename changes only a directory, and why one file can have two names. A symbolic link is the exception: it breaks if the path it holds stops existing.

About this lab and its source: Everyday Linux commands lab: the thirty commands engineers type from memory

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.