Written by
Amanda WongSummary: Read this post to understand the hard links on macOS, including what are macOS hard links, how to create a hard link on Mac, and the differences between aliases, symlinks, and hard links.
There are three types of links you may come across when working with file systems on macOS: aliases, soft links (sometimes referred to as symbolic links), and hard links. They all have the same function of linking to files or folders, yet they are all fundamentally distinct from one another.
Here, we are going to explore hard links on macOS, including the definition, the way to create a hard link, and the differences between aliases, symlinks, and hard links.
Overview of the macOS hard links
A macOS hard link is a file system feature that allows multiple filenames to refer to the same physical location on a disk. Each hard link to a file is equivalent in functionality and appearance, meaning changes made through one link are reflected in all others.
When you create a macOS hard link, you're not making a new copy of the file, both the original file and the hard link point to the same inode (the filesystem's data structure that stores information about the file). instead, you're creating a new entry for it in the file system's table, it doesn't occupy extra disk space.
Features of hard links:
- Same Inode: Hard links share the same inode with the original file, meaning they are indistinguishable from the file itself.
- Persistence: Deleting one hard link does not delete the actual data on the disk until all hard links to that data are deleted.
- Single Filesystem: Hard links cannot span across different filesystems or partitions.
- File-only: Typically, macOS does not allow hard links to directories to prevent filesystem loops.
How to create a hard link on Mac?
Creating a hard link on macOS is a straightforward process that can be done using the Terminal. Below are the steps to create a hard link on your Mac:
- Launch the Terminal app from the Finder > Applications, or search it via Spotlight.
- Type the cd command to navigate to the directory where the original file is located. cd /path/to/directory
- Enter the ln command followed by the name of the original file and the name you want to give to the hard link.ln original_file_name hard_link_nameFor example, to create a hard link of a file called `example.txt` from your Documents folder to your Desktop, you would use: ln ~/Documents/example.txt ~/Desktop/example.txt
- To verify that the hard link has been created and points to the same inode (data block) as the original file, you can use the ls command with the -i flag to display inode numbers. You should see that both files have the same inode number, indicating they reference the same data on the disk.ls -i example.txt example_hard_link.txt
Differences between hard links, symlinks, and aliases
Hard links, symlinks, and aliases are the main types of links on computers, and you may want to know the differences between them so that you can use them effectively. Actually, they serve different purposes and behave differently in macOS:
Hard Links: Unlike aliases and symlinks, a hard link is indistinguishable from the original file at the system level. It does not store any path information but directly points to the data on the disk. Hard links are useful for having multiple references to the same file without using additional disk space. However, hard links cannot span different volumes and are not suitable for linking directories.
Symlinks: Similar to aliases, symbolic links are references to a file or folder's path. However, symlinks are used at the system level and function independently of the Finder. If you move the original file, the symlink will not update to reflect the change and will break if it cannot find the original file at the specified path.
Aliases: An alias in macOS is a file that acts as a shortcut to another file or folder. Unlike hard links, aliases are managed by the Finder and can track the original file even if it's moved or renamed. Aliases are more flexible but also more dependent on the macOS Finder environment.
Share this informative post about macOS hard link!