.
and ..
are Special Hard Links to Inodes
From a purely pragmatic point of view, ..
is a hard link. On Linux, it gets treated as a hard link to the mount point when called from the top level of a mounted volume, and on Linux and macOS /
hard-links both .
and ..
to inode 2 as a special case.
More Detail
As others have pointed out, .
and ..
are both hard links to inodes on filesystems that support inodes. There are a few filesystems that don't have inodes, but that's a bit of a side issue. For practical purposes, whether a system directly supports inodes or not, a hard link just points to the same metadata block on a given filesystem. Sometimes the hard link is a real link on the filesystem, and sometimes it's synthesized because POSIX systems expect .
and ..
to exist for historical reasons. Either way, it's treated as a hard link to directories, and some filesystems such as HFS+ do allow you to hard link directories that don't cross filesystem boundaries.
Hard Linking of Directories
MacOS does allow hard links on HFS+ file systems to point to directories, because this was historically the way Time Machine optimized disk usage. However, newer versions of Time Machine use APFS filesystems and filesystem snapshots, so there's really no need for this exception except backwards compatibility, and it doesn't work on APFS. Don't rely on it.
So, it's generally the filesystem itself that determines whether or not a directory can be hard-linked or not. On Ubuntu 22.04, man 1 ln
says the ln utility supports directory links if the system allows it:
-d, -F, --directory
allow the superuser to attempt to hard link directories (note:
will probably fail due to system restrictions, even for the su‐
peruser)
The part that probably confuses most people is mount points. If you have a disk mounted as /mnt/foo
, then cd /mnt/foo; ls -ld ..
points to the inode of the mount point, which in this case would be the inode for /mnt
. Either the hard link for the ..
directory on a mount doesn't really cross filesystem boundaries because it's just looking at the hard link of /mnt/foo (which exists whether or not a volume is currently mounted there) or it gets treated specially by the kernel to support mounted volumes. This is most likely an implementation detail and I can't point to any kernel source related to it, but functionally it's still technically a hard link because ..
points to an inode rather than a soft link pointing to a file name.
You can test this for yourself with:
cd /
stat .
stat ..
On Linux, and on macOS when called with the -x
flag, you will see that both .
and ..
consistently point to inode 2. Many filesystems reserve inode 1 for bad blocks.
Since you can create hard links to directories on some filesystems, including EXT3/4 with debugfs, it's not really true that you can't hard link directories. It's generally not supported in order to ensure the directory graph is acyclic, and tools like fsck will complain about hard-linked directories other than .
and ..
on filesystems that don't support directory hard links for anything other than the current working directory (.
) and parent directory (..
) as a special legacy case.
ls -ld /usr
you will see a number after the access rights: that's the number of hard-links. (eg:drwxrwxr-x 17 root root 4096 Apr 6 17:09 /usr
) – Lorinczy Zsigmond Jun 07 '23 at 08:26..
made it a cyclic graph, then.
would also, since a node that points to itself is a cycle, no? – David Conrad Jun 08 '23 at 17:48root
could mess with.
and..
, making the filesystem inconsistent. Alsols -lid ..
will show the link count and the number of the inode (among other tinformation). – U. Windl Jun 09 '23 at 07:47