Inodes, hard links & symbolic links
Read file metadata with stat and explain how names map to inodes.
By Invicly Tech, , in Linux System Programming. About 12 minutes to read.
In short
An inode stores a file's metadata and the location of its data, but not its name. Directories are tables that map names to inode numbers, so one inode can have several names, called hard links. A symbolic link is a separate small file whose content is a path to follow.
Explained simply
A file has two parts: its name and its body. The body is the inode, a record with a number that stores the owner, permissions, size, timestamps and where the data blocks are on disk. The name lives somewhere else, in a directory. A directory is just a list of pairs: name and inode number.
Because the name is separate, one inode can have several names. Each name is a hard link, and the inode keeps a count of them. rm does not really delete a file; it removes one name. The data is freed only when no names are left and no program still has the file open.
A symbolic link (symlink) is different. It is a tiny file of its own whose content is a path, like a sticky note saying "look over there". Every time you open it, the kernel follows the path. If the target is moved or deleted, the symlink points at nothing.
This matters in practice: disk space that is not freed after rm, "No space left on device" on a disk that is half empty, and safe deployments that switch versions by changing one symlink.
Press Play on a diagram to walk through it one step at a time.
- /home/ana/report.txt
- Hard link
- /home/ana/backup.txt
- Hard link, link count is now 2
- /tmp/latest -> /home/ana/report.txt
- Symlink with its own inode
- Dangles if the target name is removed
- Default on Debian and Ubuntu
- Can grow and shrink
- Mature journaling and fsck
- Inode count fixed at mkfs time
- Default on RHEL
- Can grow but not shrink
- Strong with large files and parallel I/O
- Allocates inodes dynamically
- Another name for the same inode
- Must stay on the same file system
- Cannot point to a directory
- Keeps working after the other name is removed
- Its own small inode holding a path
- Can cross file systems and point to directories
- Breaks (dangles) if the target is removed
- Resolved again on every open
In depth
What stat tells you
stat(path, &st) fills a struct stat with the inode's metadata. lstat does the same but reports on a symlink itself instead of following it. fstat works on an open descriptor, and fstatat works relative to a directory descriptor.
The pair st_dev and st_ino identifies a file uniquely on the whole system: same device, same inode number, same file. cp uses this to refuse copying a file onto itself. st_mode holds both the file type and the permissions; test the type with S_ISREG, S_ISDIR or S_ISLNK. st_blocks counts 512-byte units, whatever the file system's block size, so blocks times 512 gives the real space used.
There are three classic timestamps. atime is the last read, mtime the last change to the content, and ctime the last change to the inode itself, including chmod or chown. ctime is not the creation time. Linux reports creation (birth) time through statx, and stat on the command line shows it as Birth when the file system stores it, as ext4 and xfs do.
- Identity
- st_dev: which file system
- st_ino: inode number
- Type and permissions
- st_mode
- Test with S_ISREG, S_ISDIR, S_ISLNK
- Owner
- st_uid
- st_gid
- Size
- st_size: bytes
- st_blocks: 512-byte units used
- Links
- st_nlink: number of names
- Times
- st_atim: read
- st_mtim: content changed
- st_ctim: inode changed
Link counts and when data is really freed
link (the ln command) adds a name and increases st_nlink. unlink (rm) removes a name and decreases it. The kernel frees the inode and its blocks only when two things are both true: the link count is zero, and no process has the file open or mapped. That is why deleting a huge log that a service still writes to frees nothing until the service closes it or restarts.
Programs use this on purpose. Create a temporary file, unlink it right away, and keep using the descriptor: the file has no name, nobody else can open it, and it disappears automatically when the program exits, even after a crash. Linux's O_TMPFILE creates such a file without ever giving it a name.
On ext4 and xfs a directory's link count is 2 plus the number of subdirectories: its own name, its . entry, and the .. of each child. rename within one file system is atomic; across file systems it fails with EXDEV, and mv falls back to copy and delete.
- 1Create a.txt
Link count 1
- 2ln a.txt b.txt
Link count 2, same inode
- 3rm a.txt
Link count 1; b.txt still works
- 4rm b.txt while open
Count 0, but readable through the fd
- 5Last close
Now the blocks are freed
Symlinks in depth
A symlink stores a path as text. A relative target is resolved from the directory that contains the link, not from your current directory, which surprises many people. readlink returns that stored text but does not add a terminating NUL byte, so you must add it yourself.
The kernel follows at most 40 symlinks while resolving one path and then fails with ELOOP, which stops loops. On Linux a symlink's own permissions are always shown as lrwxrwxrwx and are ignored; the target's permissions are what count.
Symlinks are popular for deployments: current points to releases/v41, and switching versions means pointing it at releases/v42. To make the switch atomic, create the new link under a temporary name and rename it over the old one, which mv -T does. Recent GNU ln -sfn also uses a temporary name, but older and non-GNU versions remove the old link first, leaving a moment with no link at all.
- readlink -f path: resolve every link to the final path
- O_NOFOLLOW makes open fail on a symlink
- fs.protected_symlinks blocks some /tmp symlink attacks
Inodes on ext4 and xfs
ext4 creates its inode table when the file system is made. By default mke2fs creates one inode for every 16 KiB of space, and that number never changes afterwards. A disk full of tiny files, such as a mail spool or a cache, can run out of inodes while df -h still shows plenty of free space. New files then fail with ENOSPC. df -i shows inode usage, and the fix is to delete files or recreate the file system with mkfs.ext4 -i or -N.
xfs allocates inodes dynamically as needed, up to a configurable share of the space, so it rarely runs out. Default inode sizes are 256 bytes on ext4 and 512 bytes on xfs. Small symlink targets and tiny directories can be stored right inside the inode, which saves a disk read.
Learn it in 4 short steps
About 45m each. Track them with a timer in your plan.
- 1Read metadata with stat and fstat~45m
- 2Create hard and symbolic links~45m
- 3Explain link counts and unlink~45m
- 4Compare ext4 and xfs at a high level~45m
Key points
- stat shows the inode number, link count, size, permissions and timestamps.
- unlink removes a name; data is freed only when no names and no open descriptors remain.
- Hard links cannot cross file systems or point to directories.
- Symlinks can cross file systems and dangle when the target disappears.
Key terms
Tap a card to flip it. 0/6 known
Examples
echo hi > a.txt
ln a.txt b.txt # hard link
ln -s a.txt c.txt # symbolic link
ls -li a.txt b.txt c.txt
stat -c "%i %h %n" a.txt
rm a.txt; cat b.txt # still prints hi
cat c.txt # No such file or directorySame inode number for a.txt and b.txt; c.txt breaks when a.txt goes.
Why is the disk still full after rm?
A 500 MB log was deleted, but df shows no free space gained. Step 1: reproduce it with a big file that tail keeps open. Step 2: lsof +L1 lists open files whose link count is 0, the deleted-but-open ones. Step 3: /proc/PID/fd shows the same file marked (deleted). Step 4: free the space either by restarting the process or by truncating the file through its descriptor, which empties it without needing the process to cooperate. Check the descriptor number first; it is not always 3.
dd if=/dev/zero of=big.log bs=1M count=500
tail -f big.log > /dev/null & # keeps the file open
rm big.log
df -h . # the space is NOT freed yet
lsof +L1 | grep big.log # NLINK column shows 0
ls -l /proc/$!/fd | grep deleted # e.g. 3 -> /home/ana/big.log (deleted)
: > /proc/$!/fd/3 # truncate it through the open descriptor
kill %1 # or stop the process: blocks are freedSwitch a release atomically with a symlink
Your web server serves files from current, a symlink. Step 1: point current at the first release. Step 2: to deploy v2, create a second symlink with a temporary name. Step 3: rename it over current with mv -T; rename is atomic, so every request sees either v1 or v2, never a missing path. Step 4: roll back the same way by pointing a new temporary link at v1.
mkdir -p releases/v1 releases/v2
ln -s releases/v1 current
ls -l current # current -> releases/v1
ln -s releases/v2 current.new
mv -T current.new current # one rename(2): atomic switch
readlink current # releases/v2Quick reference
- stat -c '%i %h %s %b %n' f prints inode, link count, size and 512-byte blocks
- ls -li shows inode numbers; the same number on one file system means hard links
- find /home -xdev -samefile f finds every hard link to f
- df -i shows free inodes; IUse 100% means no new files even with free space
- lsof +L1 lists files that were deleted but are still open
- readlink -f path resolves every symlink to the final absolute path
- mv -T current.new current replaces a symlink atomically
- st_blocks counts 512-byte units, not file system blocks
Common mistakes
- Expecting rm to free disk space while a process still has the file open.
- Using a relative symlink target that resolves differently from the link's directory.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Create a file, give it a second hard link and a symlink, and show the inode numbers and link counts. Then remove the original name and explain what still works.
- Exercise 2medium
Write mystat: for each path on the command line, print its type (file, dir, symlink or other), inode number, link count and size. Do not follow symlinks.
- Exercise 3hard
Create a temporary file with mkstemp, unlink it at once, write 1 MiB into it, and use fstat to show it has 0 links but real blocks. Explain when the space is freed.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- What happens if I delete a file a program is still using?
- Only the name disappears. The program keeps reading and writing normally, and the space is freed when it closes the file or exits.
- Can I create a hard link to a directory?
- No. Linux refuses with EPERM, because directory hard links could create loops in the tree. Use a symlink or a bind mount instead.
- Does a symlink have its own permissions?
- On Linux it always shows lrwxrwxrwx, and those bits are ignored. Access is decided by the permissions of the target and of the directories on the way.
- How do I find all hard links to a file?
- Run find on the same file system with -samefile file or -inum followed by the inode number. Hard links can never be on another file system, so -xdev keeps the search short.