What Is a Hard Link in Linux? A Practical Guide to Hard Links vs. Soft Links
If you see invalid cross-device link while trying to create a link in Linux, the problem is usually not your command syntax. It is the filesystem boundary, the link type, or both.
This guide explains what a hard link is in Linux, how it differs from a soft link, why the hard link vs symbolic link decision matters, and when that invalid cross-device link error shows up in real work. You will also learn how to create a hard link, verify it with terminal commands, and avoid the common mistakes that break scripts or cause data confusion.
At a practical level, a hard link is not a duplicate file. It is another directory entry pointing to the same inode. A soft link, also called a symbolic link, is a path reference. That one difference explains most of the behavior people trip over when they ask, can hard links be created to directories in linux, can hard links point to directories in linux, or can you create hard link to directory in linux.
A hard link is another name for the same file content. A soft link is a pointer to where the file is supposed to be.
That distinction affects storage, deletion behavior, backups, and even how you interpret what is a hard reset in a Linux troubleshooting workflow. A reset may restore system state, but a link relationship depends on inode behavior, not “resetting” the file itself.
What a Hard Link Is in Linux
A hard link is an additional directory entry for an existing file. It does not create another copy of the file contents. It simply gives the same data another name in the filesystem.
Linux files are identified by inodes, not by their filenames. When you create a hard link, you are adding a new filename that points to the same inode as the original file. That is why two hard-linked files are effectively the same object from the filesystem’s perspective.
This is why editing one name changes the other name too. The content is stored once, and both names reference that same content. If you open one linked file in vim or append to it with echo, the change appears everywhere that inode is referenced.
Why hard links matter in daily Linux work
Hard links are useful when you need two names for one file without consuming extra storage. That can help with local file organization, staged workflows, simple version retention, and recovery scenarios where you want a file to remain accessible under one name while another process uses another.
- No duplicate storage for the same file content.
- Same content, multiple names for organizational flexibility.
- Deletion safety as long as one link still exists.
- Predictable behavior once you understand inodes.
Note
Hard links are common in Unix-style filesystems because the filesystem tracks file identity through inodes. The Linux man pages for ln, stat, and related utilities are the most reliable starting point for command behavior. See man7 ln and man7 stat.
For a real-world mental model, think of a hard link like multiple office labels on the same filing cabinet drawer. The labels differ. The drawer does not.
Understanding Inodes in Linux
An inode is the filesystem structure that stores metadata about a file. It includes permissions, ownership, timestamps, file size, and file type. What it does not store is the filename itself.
The name you type in the terminal lives in a directory entry. That directory entry maps a name to an inode number. This separation is the reason hard links exist at all. One inode can be referenced by many directory entries.
When people ask how hard links work behind the scenes, the answer is simple: the directory name is a label, and the inode is the actual file identity. If two names have the same inode number, they are hard-linked to the same file content.
How to inspect inode numbers
Use ls -li to see inode numbers and link counts:
ls -li report.txt report-copy.txt
If both names show the same inode number, they point to the same file. You can also use stat for a fuller picture:
stat report.txt
The output typically shows the inode, number of links, file type, permissions, and timestamps. That makes it easier to confirm whether you are dealing with a copy, a hard link, or a symbolic link.
- Same inode = same underlying file.
- Link count = number of directory entries pointing to that inode.
- Different inode = separate file, even if the content matches.
Inode numbers are also useful when troubleshooting accidental duplication. If two files look different by name but share the same inode, they are not separate data objects. They are two labels on one file.
That is why inode-based storage is central to Linux filesystem behavior and why the question “can hard links point to directories in linux” leads to edge-case rules instead of a simple yes. Directories create structural risks in inode-linked trees, which is why Linux restricts them in normal use.
How Hard Links Work Behind the Scenes
A Linux filesystem separates three things: the directory entry, the inode, and the data blocks. The directory entry stores the filename. The inode stores metadata. The data blocks store the actual content.
Creating a hard link does not copy the data blocks. It creates another directory entry that points to the same inode. That is why a hard link uses almost no extra storage and why a rename does not break the relationship between linked names.
The key concept is the link count. Every time a hard link points to an inode, the inode’s link count goes up by one. When you delete one link, the count drops, but the data remains until the last link is removed.
Deletion behavior is the part people get wrong
If you remove one hard link with rm, the file content is not immediately gone. Linux only releases the data blocks when the final link count reaches zero and no process still has the file open.
- Create a file.
- Create a hard link to it.
- Delete one filename.
- Keep using the remaining filename.
That sequence surprises new admins because the content seems to “survive” deletion. It is not magic. It is inode reference counting.
Key Takeaway
Deleting a hard link removes only one directory entry. The data disappears only when the last link to the inode is gone and no process holds the file open.
This behavior is useful in maintenance windows and cautious file replacement workflows. You can preserve access through one filename while another process rotates or stages the same content under another name.
Official filesystem behavior varies slightly by filesystem type, so it is smart to check the docs for the filesystem in use. For broader Linux filesystem guidance, Red Hat’s documentation on filesystem management is a useful reference: Red Hat Documentation.
Creating Hard Links with the ln Command
The standard Linux tool for creating hard links is ln. For hard links, you use the command without -s. The -s option creates a symbolic link instead.
Basic syntax:
ln source_file link_name
Example:
echo "Quarterly report draft" > report.txt
ln report.txt report-link.txt
Now both names point to the same inode. If you edit report-link.txt, you are editing report.txt as well.
What to check before you run ln
The source must already exist. You are not creating a new file from nothing. You are adding a new name for an existing file. That is an important distinction when scripting or automating file operations.
- Source must exist before the hard link is created.
- Destination must not already be the same name unless you intend to overwrite a path in a controlled way.
- Use hard links for same-filesystem references, not for portability.
- Use symbolic links when you need a pointer to a path instead of a shared inode.
Common workflow use cases include keeping a stable filename while another process writes to a different name, preserving access to a file during cleanup tasks, or creating a lightweight local alias without duplication. Those are practical reasons admins and developers reach for hard links.
For the official command behavior, check the GNU Coreutils documentation and your system man page. The general reference for GNU ln is available through GNU Coreutils.
Verifying Hard Links in the Terminal
Never assume a file is hard-linked just because the names look related. Verify it. The fastest method is ls -li, which shows inode numbers and link counts side by side.
ls -li report.txt report-link.txt
If the inode numbers match, the files are hard links to the same inode. If the link count is 2 or more, that inode has multiple directory entries pointing to it.
What to look for in ls -li output
- Same inode number across files = hard link relationship.
- Link count greater than 1 = more than one directory entry exists.
- Different inode numbers = separate files, even if their content is identical.
The stat command gives a fuller view:
stat report.txt
You will usually see the inode number, number of hard links, file size, access mode, and timestamps. That helps during incident response or troubleshooting when file behavior does not match what the names suggest.
Two files with the same contents are not necessarily hard links. Hard links share an inode. Copies do not.
This verification step matters in operations work. If a deployment script creates a copy instead of a link, disk usage goes up. If a cleanup job removes the wrong filename, you may think a file disappeared when it actually remains available under another hard link.
For Linux filesystem and command-line reference, the man7.org Linux man-pages collection remains one of the most trusted sources for precise behavior.
Hard Links vs. Soft Links
A soft link or symbolic link is a separate file that stores a path to another file or directory. It does not share the target’s inode. That is the core difference in the hard link vs symbolic link comparison.
Hard links point to the same inode. Symbolic links point to a path. Because of that, a symbolic link can break if the target path changes or the target file is removed. A hard link remains valid as long as at least one link to the inode still exists.
Behavior when the target is renamed or deleted
If you rename the original target of a symbolic link, the symlink may stop working because the stored path no longer resolves. If you delete the original name of a hard-linked file, the data still exists through the remaining linked name.
| Hard link | Points to the same inode and stays valid until the final link is removed. |
| Soft link | Points to a path and can break if that path changes or disappears. |
That difference explains why symbolic links are more common for application shortcuts, mount-like references, and cross-directory indirection. Hard links are more about identity than location.
When people ask, what is the difference between same file and pointer to location, this is the answer. A hard link is the same file. A soft link is a path reference to another file.
For authoritative Linux command behavior, see the ln man page and your distribution documentation. If you work in Microsoft or mixed-environment systems, Microsoft’s filesystem and command documentation can help when you are comparing file-link behavior across platforms: Microsoft Learn.
Key Differences in Behavior and Use Cases
The most practical way to choose between a hard link and a symbolic link is to compare how they behave in day-to-day tasks. The storage model, filesystem limits, and failure modes are different.
Hard link vs. symbolic link at a glance
| Storage | Hard links share the same data blocks through one inode; symbolic links store a separate path. |
| Filesystem boundary | Hard links usually cannot span filesystems; symbolic links can point across filesystems. |
The cross-filesystem limitation is the reason users often see invalid cross-device link. If you try to create a hard link across mounted filesystems, Linux rejects it. That error is expected. The source and destination are on different devices, so the inode cannot be shared across them.
That limitation also explains why the command works on some paths but not others. A link that succeeds within /home may fail when the destination lives on a different mounted volume such as /mnt/data.
Why hard links cannot normally be created for directories
Users often search for can hard links be created to directories in linux or can hard links point to directories in linux because the restriction feels arbitrary. It is not. Allowing directory hard links would make loops and filesystem traversal problems much more likely. Backup tools, recursive commands, and filesystem checks could become unstable.
That is why Linux generally prevents normal users from creating hard links to directories. The question can you create hard link to directory in linux is usually answered with “not in normal practice.” Symbolic links are the safer tool for directory references.
- Hard links are best for same-filesystem file identity.
- Soft links are best for flexible path references.
- Directories generally use symbolic links, not hard links.
- Cross-device references require symbolic links or another approach.
For deeper technical standards on filesystem behavior and security implications, MITRE ATT&CK is a useful framework for understanding how path manipulation and link abuse can appear in adversarial activity: MITRE ATT&CK.
Hard Link Limitations and Common Pitfalls
Hard links are simple once you understand them, but they create trouble when people use them like copies. The biggest mistake is assuming that a hard link isolates changes. It does not.
If you edit one hard link, you edit all of them. If a process expects one name to be disposable, but another hard link points to the same inode, deletion will not behave the way that process expects. That can break cleanup scripts or retention rules.
Common pitfalls to avoid
- Assuming a hard link is a copy when it is really shared data.
- Using hard links across filesystems and triggering invalid cross-device link.
- Trying to hard-link directories in normal workflows.
- Forgetting link counts and deleting the wrong name.
Hard links also do not work the same way for every special file type. Device files, sockets, and other non-regular file types can behave differently depending on filesystem and permissions. In most everyday admin tasks, hard links are meant for regular files.
Warning
If a script assumes a hard link is a separate file, it can overwrite shared content, retain stale data, or delete a file that another workflow still needs. Always verify inode numbers before using linked files in automation.
For security and filesystem risk management, the NIST guidance on secure configuration and file integrity concepts is useful background, especially when link behavior affects controls, logging, or file retention.
The bottom line: hard links are efficient, but they are not forgiving. Use them only when shared identity is what you want.
Practical Linux Scenarios Where Hard Links Help
Hard links are less common than symbolic links, but they solve a few real problems cleanly. One example is preserving a file under one stable name while another process works with a different name in the same filesystem.
Another example is lightweight version handling. If you need two names for the same content during a transition period, a hard link lets you avoid copying large files. That is useful on smaller systems, development boxes, and constrained storage environments.
Where hard links make sense
- Local archival workflows where you want one file to appear under multiple names.
- Staged changes where you need a safe rename path within one filesystem.
- Low-storage systems where duplicating large files is wasteful.
- Testing workflows where two names must behave identically.
System administrators also use hard links during maintenance tasks when they need to keep a file accessible while replacing another name in place. That can reduce downtime and avoid copying large datasets unnecessarily.
Still, hard links are less common than symbolic links in everyday work because symbolic links are more flexible. They can cross filesystems, they can point to directories, and they match how humans usually think about shortcuts.
Use hard links when identity matters. Use symbolic links when location matters.
For filesystem and data-handling practices, the Linux Foundation and vendor documentation are useful references, but for link behavior itself, the most dependable source is still the Linux man pages and your distribution’s filesystem docs. If you are evaluating storage efficiency or file management strategies at scale, BLS occupational data and industry research such as BLS Occupational Outlook Handbook can help frame why Linux administration skills remain relevant across infrastructure roles.
Best Practices for Working with Links in Linux
The safest way to work with links is to treat them as part of your file design, not as a shortcut you add later without documentation. A few habits prevent most problems.
Practical rules that prevent mistakes
- Use descriptive names so related links are easy to identify.
- Check inode numbers before assuming two files are separate copies.
- Prefer symbolic links when files may move across filesystems.
- Document link relationships in scripts, runbooks, or admin notes.
- Test deletion and rename behavior before relying on a link in production.
If you are writing scripts, do not guess. Use commands like ls -li, stat, and readlink where appropriate. Validate the result after creating or changing a link, especially when automated jobs might remove or rotate files later.
For example, if a backup job keeps the same filename through hard links, another process that deletes the “old” file might not free space the way you expect. That is normal hard-link behavior, but it can confuse storage reporting or retention logic if nobody documents it.
Pro Tip
When testing link behavior, create a small text file first. Change it, rename it, and delete one name at a time. You will learn more in five minutes of hands-on testing than in an hour of reading alone.
For file integrity, access control, and secure handling, many organizations map these behaviors back to operational controls. If links affect audit trails or data retention, consult relevant policy and control frameworks such as ISO 27001/27002 or NIST guidance, then align your filesystem practices with them.
What to Remember About Invalid Cross-Device Link Errors
The invalid cross-device link error is one of the clearest signs that you are trying to create a hard link where Linux cannot support one. This usually happens when the source and destination live on different mounted filesystems.
That is the first thing to check when ln fails. Run df -h or mount to confirm whether the paths are on different devices. If they are, use a symbolic link instead, or move both paths onto the same filesystem if a hard link is required.
A quick troubleshooting checklist
- Confirm the source file exists.
- Check whether source and destination are on the same filesystem.
- Verify you are not trying to hard-link a directory.
- Use
ls -lito compare inode numbers after creation. - Switch to
ln -sif you need a path-based reference across devices.
That error is not a bug. It is Linux telling you the link type does not match the storage layout. Once you understand that, the fix is usually obvious and fast.
For broader operational context, modern security and systems teams increasingly document file behavior because filesystem mistakes can affect restore procedures, incident response, and automation reliability. Reference sources such as CISA and NIST help align technical behavior with operational controls.
Conclusion
A hard link in Linux is another name for the same file content through the same inode. That is the core idea to remember. A soft link points to a path, which is why symbolic links are more flexible but also more fragile when the target moves or disappears.
If you remember only three commands, make them ln, ls -li, and stat. Those are enough to create a hard link, verify it, and explain its behavior to another admin or developer without guessing.
Use hard links when you need shared file identity on the same filesystem. Use symbolic links when you need portability, directory references, or cross-filesystem shortcuts. And when you see invalid cross-device link, check the filesystem boundary first.
For further reading, rely on authoritative sources: the Linux man pages, GNU Coreutils, and your platform documentation. That habit will save time, reduce mistakes, and make your Linux file management work more predictable.
CompTIA®, Cisco®, Microsoft®, AWS®, EC-Council®, ISC2®, ISACA®, and PMI® are trademarks of their respective owners.
