A hard link lets multiple filenames point to the same file data through the same inode. Creating a hard link does not copy file contents. It adds another directory entry that references the same underlying inode.
This is why hard links can save disk space and why tools such as pnpm can reuse package content efficiently.
What an inode is
An inode stores file metadata and pointers to file data: size, permissions, owner, timestamps, and where the data lives on disk. A filename is a directory entry that points to an inode.
When you create a hard link, the filesystem creates another filename pointing to the same inode.
Basic example
echo "hello" > original.txt
ln original.txt linked.txt
ls -li original.txt linked.txt
The -i option shows inode numbers. Both names should show the same inode.
If you edit one:
echo "updated" > linked.txt
cat original.txt
The other name sees the updated content because both names refer to the same file data.
Link count and deletion
Each inode has a link count. Deleting one filename removes one directory entry and decreases the count. The file data is removed only when the link count reaches zero and no process keeps it open.
That means:
rm original.txt
cat linked.txt
can still work because linked.txt still points to the inode.
Hard link characteristics
- Hard links share the same file content.
- There is no meaningful "original" after links exist; both names are peers.
- Deleting one link does not delete the data while another link remains.
- Hard links normally cannot cross filesystem boundaries.
- Hard links to directories are usually disallowed to avoid loops and complexity.
Hard link vs symbolic link
| Feature | Hard link | Symbolic link |
|---|---|---|
| Points to | inode / file data | path string |
| Cross filesystem | usually no | yes |
| Directory support | usually no | yes |
| Target deletion | other hard links still work | symlink can become broken |
| Separate file | no, another name for same inode | yes, a small link file |
Use symbolic links when you need a pointer to a path, especially across filesystems or for directories. Use hard links when multiple names should refer to the same file content on the same filesystem.
Why pnpm uses hard links
Traditional dependency installs can duplicate the same package across many projects. pnpm stores package content in a global content-addressable store and links it into projects, which can save disk space and speed up installs.
Conceptually:
global pnpm store/package-content
project-a/node_modules -> linked content
project-b/node_modules -> linked content
For practical pnpm usage, read pnpm in Practice: Installation, Migration, and Workspaces.
When hard links are useful
- Efficient local package storage.
- Backup systems that keep unchanged files without copying data.
- Multiple names for the same file content within one filesystem.
- Some deployment and cache strategies.
Do not use hard links as a substitute for version control, backups, or permission design. They are a filesystem mechanism with specific limits.
Related FreeMac guides
- For pnpm tradeoffs, read pnpm vs npm vs Yarn: Which Package Manager Should You Use?.
- For pnpm migration, read Replacing npm with pnpm: A Complete Migration Guide.
- For Linux commands, read Linux Commands for Files, Processes, Network, and Logs.
Continue reading
Linux Commands for Files, Processes, Network, and Logs
A practical Linux command guide for developers: files, directories, logs, processes, network checks, SSH, curl, environment variables, and safer cleanup habits.
10 min readNginx Reverse Proxy Guide: proxy_pass, Headers, WebSocket, and Logs
Understand reverse proxies and configure Nginx for Node.js, Next.js, or Strapi with proxy_pass, forwarded headers, WebSocket upgrades, timeouts, and debugging logs.
9 minpnpm in Practice: Installation, Migration, and Workspaces
Use pnpm for faster installs, smaller dependency storage, stricter resolution, npm migration, and practical workspace management across frontend projects.
Subscribe to FreeMac
Weekly picks: free Mac software reviews, trusted source updates, alternatives, and low-friction guides.