In Unix-based systems, like Linux and macOS, links are an integral part of the file system structure. Links create connections between files and directories, making it possible to access a file or directory from multiple locations. This article will delve into the three main types of links you’ll encounter in the terminal: symbolic links (symlinks), hard links, and firmlinks. We’ll cover what they are, how they work, and how to use them.
A hard link is a reference, or pointer, to physical data on a disk. When a hard link is created, it points to the same inode as the original file, essentially creating another name for the same file content. Here’s how to create a hard link:
ln sourcefile hardlink
In this command,
sourcefile is the existing file that you’re linking to, and
hardlink is the name of the new hard link. After running the command, both
hardlink point to the same data.
Hard links have a few key characteristics:
- Hard links share the same inode and data with the original file. As a result, they appear as though they are the same file.
- A change in the content of either the hard link or the original file is reflected in both.
- Hard links can’t link to directories to prevent circular linking.
- Hard links can’t span different file systems or partitions.
Symbolic Links (Symlinks)
A symbolic link, also known as symlinks or a soft link, is a special type of file that serves as a reference to another file or directory. You can think of symbolic links like an alias in Finder, or a shortcut in Windows. Unlike a hard link, a symlink is a separate file that points to the location of the original file.
Here’s how to create a symlink:
ln -s target symlink
In this command,
target is the existing file or directory that you’re linking to, and
symlink is the name of the new symbolic link.
Symbolic links have the following characteristics:
- Symlinks can point to any file or directory, regardless of the filesystem or partition.
- Symlinks can link to directories, unlike hard links.
- If the original file is deleted, the symlink becomes a ‘dangling’ link that points to a non-existent file.
Firmlinks are a unique type of link introduced in macOS Catalina with the advent of the read-only system volume. A firmlink is a bi-directional wormhole between the read-only system volume and the writable data volume. It makes two directories appear as one to the file system.
The firmlinks themselves are defined in a file located at
/usr/share/firmlinks. Unfortunately, this isn’t something you can modify as a user or developer. Firmlinks are used only by Apple to maintain compatibility in their new file system layout.
Understanding links, especially symbolic links, is a crucial part of mastering file system management in Unix-based systems. While you may not use these commands daily, knowing how to create and manage links will enable you to craft advanced commands and scripts and better understand how your file system works. Firmlinks, while not directly usable, show how Apple has advanced its file system technology to maintain compatibility and separate system and user data more effectively.