TExcelTAD
Home › Operating Systems: Linux & Windows › Links, Inodes & File Metadata

Links, Inodes & File Metadata

In File System Concepts you saw that a file's name and the file itself are stored separately: a directory entry maps a name to an inode number, and the inode holds everything else. This split has a surprising consequence: one file can have several names, and a name can exist without a file. This lesson explores hard links, symbolic links and the metadata the kernel keeps for every file, on Linux and on Windows.

What an inode records

stat prints a file's inode. Here we give the file a fixed timestamp with touch -d so the output is reproducible (the sandbox runs in UTC):

bash · try it yourself
printf 'hello\n' > note.txt
chmod 640 note.txt
touch -d '2026-01-15 10:00:00' note.txt
stat -c 'name:  %n
size:  %s bytes
type:  %F
mode:  %A (%a)
owner: %U (uid %u), group %G (gid %g)
links: %h
mtime: %y' note.txt
name:  note.txt
size:  6 bytes
type:  regular file
mode:  -rw-r----- (640)
owner: learner (uid 1000), group learner (gid 1000)
links: 1
mtime: 2026-01-15 10:00:00.000000000 +0000
Field Meaning
inode number The file's identity within its file system (ls -i, stat -c %i)
type Regular file, directory, symlink, block/character device, FIFO, socket
mode Permission bits plus setuid/setgid/sticky (Linux Users & Permissions)
uid, gid Owner and group, stored as numbers; names come from /etc/passwd
size, blocks Bytes of content, and disk space actually allocated
link count How many directory entries point to this inode
atime, mtime, ctime Last access, last modification of content, last change of the inode itself
btime Birth (creation) time, on ext4/XFS/Btrfs with a recent kernel

Note ctime is change time, not creation time: a chmod updates ctime but not mtime. And note the name is printed by stat only because you typed it; it is not inside the inode.

Hard links: more names for the same inode

ln target newname creates a second directory entry pointing to the same inode. Neither name is the "original"; both are equally real, and the link count goes up:

bash · try it yourself
echo "version 1" > report.txt
ln report.txt backup.txt
stat -c '%h %F %n' report.txt backup.txt
[ report.txt -ef backup.txt ] && echo "same inode"

echo "version 2" > report.txt          # write through one name...
cat backup.txt                         # ...and read it through the other

rm report.txt                          # removes one name only
stat -c '%h %F %n' backup.txt
cat backup.txt
2 regular file report.txt
2 regular file backup.txt
same inode
version 2
1 regular file backup.txt
version 2

The data is freed only when the link count reaches 0 and no process still has the file open. That second condition is why deleting a huge log file that a running service keeps open does not free disk space until the service restarts (lsof +L1 finds such files).

Hard links have two limits:

  • They cannot cross file systems: an inode number only means something inside one file system.
  • You cannot hard-link a directory (it could create loops in the tree). The kernel itself makes the only directory links, . and ...

That is why a directory's link count is 2 + number of subdirectories: its name in the parent, its own ., and the .. of each child:

bash · try it yourself
mkdir -p project/src project/docs project/tests
stat -c '%h %F %n' project project/src
5 directory project
2 directory project/src

Symbolic links: a name that points to a path

A symbolic link (symlink, soft link) is a tiny file of its own whose content is a path. The kernel follows that path when you open the link. Symlinks can point anywhere, across file systems, to directories, even to things that do not exist:

bash · try it yourself
echo "settings" > app.conf
ln -s app.conf current.conf
ln -s /etc/os-release os-info
stat -c '%h %F %n' app.conf current.conf
readlink current.conf
cat current.conf
head -n 1 os-info

rm app.conf
cat current.conf 2>&1 || true
stat -c '%F: %N' current.conf
[ -L current.conf ] && [ ! -e current.conf ] && echo "dangling symlink"
1 regular file app.conf
1 symbolic link current.conf
app.conf
settings
NAME="Alpine Linux"
cat: current.conf: No such file or directory
symbolic link: 'current.conf' -> 'app.conf'
dangling symlink

The link count of app.conf stayed at 1: a symlink does not count as a reference. When the target is removed, the symlink survives and dangles.

Hard link Symbolic link
What it is Another directory entry for the same inode A separate small file containing a path
Across file systems No Yes
To directories No Yes
If the target is deleted Data stays (it is the target) Link dangles
If the target is moved Still works Breaks (relative links survive moving both together)
ls -l shows Just a normal file, link count > 1 l type and name -> target
Typical uses Backups with deduplication (rsync --link-dest), package stores /bin -> usr/bin, current -> releases/v42, config switching

Modelling link counts

This program models the rules above: directory entries, a link count per inode, open file handles, and the moment the data is really freed:

python · try it yourself
inodes = {}       # ino -> {"links": n, "open": n, "data": str}
dirents = {}      # name -> ino (one directory, for simplicity)
next_ino = 100

def create(name, data):
    global next_ino
    next_ino += 1
    inodes[next_ino] = {"links": 1, "open": 0, "data": data}
    dirents[name] = next_ino

def link(old, new):
    dirents[new] = dirents[old]
    inodes[dirents[new]]["links"] += 1

def maybe_free(ino):
    node = inodes[ino]
    if node["links"] == 0 and node["open"] == 0:
        del inodes[ino]
        print(f"   inode {ino} freed")

def unlink(name):
    ino = dirents.pop(name)
    inodes[ino]["links"] -= 1
    print(f"rm {name}: links={inodes[ino]['links']} open={inodes[ino]['open']}")
    maybe_free(ino)

def close(ino):
    inodes[ino]["open"] -= 1
    print(f"close inode {ino}")
    maybe_free(ino)

create("app.log", "lots of log lines")
link("app.log", "app.log.bak")
log = dirents["app.log"]
inodes[log]["open"] += 1                      # a service keeps the log open
unlink("app.log")
unlink("app.log.bak")
print("names left:", list(dirents), "| inode still on disk:", log in inodes)
close(log)                                    # the service restarts
print("inode still on disk:", log in inodes)
rm app.log: links=1 open=1
rm app.log.bak: links=0 open=1
names left: [] | inode still on disk: True
close inode 101
   inode 101 freed
inode still on disk: False

Both names were gone after the second rm, yet the inode lived on until the last handle was closed. On Linux, the open-but-deleted file is still visible as /proc/<pid>/fd/<n> -> /var/log/app.log (deleted).

Links on Windows (NTFS)

NTFS supports the same ideas plus one Windows-specific kind. All are created with mklink, a cmd built-in (sample session; symlinks need an elevated prompt or Developer Mode):

cmd
C:\Users\ada\demo>mklink /H backup.txt report.txt
Hardlink created for backup.txt <<===>> report.txt

C:\Users\ada\demo>mklink current.conf app.conf
symbolic link created for current.conf <<===>> app.conf

C:\Users\ada\demo>mklink /D docs C:\Users\ada\Documents
symbolic link created for docs <<===>> C:\Users\ada\Documents

C:\Users\ada\demo>mklink /J proj D:\work\project
Junction created for proj <<===>> D:\work\project

C:\Users\ada\demo>fsutil hardlink list report.txt
\Users\ada\demo\report.txt
\Users\ada\demo\backup.txt

PowerShell does the same with New-Item -ItemType HardLink|SymbolicLink|Junction -Path … -Target ….

Windows Linux counterpart Notes
Hard link (mklink /H) ln Files only, same volume; extra $FILE_NAME attribute in the MFT record
File symlink (mklink) ln -s A reparse point; needs SeCreateSymbolicLinkPrivilege (admin) or Developer Mode
Directory symlink (mklink /D) ln -s to a directory Can point to relative paths and network shares
Junction (mklink /J) (bind mount, roughly) Directories only, absolute local paths, no admin needed; used by Windows itself for compatibility folders
Shortcut (.lnk) .desktop file Just a file Explorer understands; the kernel does not follow it

A reparse point is NTFS's general hook: a tag plus data on a file that tells the I/O manager "do something special here". Symlinks and junctions are reparse points; so are OneDrive's cloud-only placeholders, deduplicated files and WSL's Unix sockets.

Git checkouts show the difference in practice: a repository containing symlinks gets real symlinks on Linux and macOS, but on Windows Git writes plain text files with the target path unless core.symlinks=true and symlink creation is allowed.

Learn more: File System Concepts · Bash: chmod & chown · NTFS & Windows File Systems

Test yourself

You run ln a.txt b.txt, then rm a.txt. What happens to the data?

  1. It is deleted
  2. It stays, reachable as b.txt, with link count 1
  3. b.txt becomes a dangling link
  4. rm refuses to delete a linked file

Which kind of link can point to a directory on another file system?

  1. A hard link
  2. A symbolic link
  3. Neither
  4. Both

Which NTFS link type can a normal (non-admin) Windows user create without Developer Mode?

  1. File symbolic link
  2. Directory symbolic link
  3. Junction
  4. None of them