355 字
2 分钟
Playing with Symbolic Link Loops on macOS

Playing with Symbolic Link Loops on macOS#

Most of the time, symbolic links (symlinks) are one of the handiest features of Unix-like systems. They let you create shortcuts that act like real files or folders. For example:

ln -s /private/tmp ~/tmp

Now ~/tmp works as if it were the real /private/tmp folder. Both Finder and Terminal happily follow the link.

But what happens if you try to get clever — and point a symlink back to itself?


In my test directory, I created a folder a and then a symlink tmp_a:

mkdir a
ln -s a tmp_a

At this point, tmp_a correctly pointed to the folder a.

Then I moved things around and re-linked tmp_a… back to itself:

mv tmp_a ../
ln -s tmp_a tmp_a

Listing the directory now shows:

lrwxr-xr-x 1 lishuyu wheel Oct 2 18:33 tmp_a -> tmp_a

That’s a symlink pointing to itself — a perfect loop.


What Happens Next?#

  • Finder:

    Clicking on the alias gives a warning:

    “The operation can’t be completed because the original item for ‘tmp_a’ can’t be found.”

    Finder refuses to open it.

  • Terminal:

    Trying to cd into the link gives:

cd: too many levels of symbolic links: tmp_a
  • The system detects the infinite recursion and blocks you.

So neither Finder nor the shell falls into an infinite loop — they both error out safely.


Why This Matters#

This experiment shows how symlinks are robustly handled at the OS level. Even if you create a circular chain like:

tmp_a -> tmp_b
tmp_b -> tmp_a

you’ll hit the same “too many levels of symbolic links” error. Tools like ls -l reveal the loop, and traversal stops.

It’s harmless to test, but pointless to keep. Recursive symlinks confuse software, break scripts, and don’t give you anything useful.


Takeaway#

  • Aliases (Finder shortcuts) and symlinks (Unix links) look similar but behave differently.

  • A symlink loop won’t crash your system, but it won’t work either.

  • If you want multiple paths to the same folder, point them all to the real target, not to each other.


👉 So next time you’re curious about the limits of symlinks: yes, you can make them point to themselves. No, it won’t break macOS. But you’ll just end up with a useless link and a funny error message.


[!quote] Patience is the companion of wisdom. — Saint Augustine

Playing with Symbolic Link Loops on macOS
https://blog.lishuyu.top/posts/infinte_loop_of_symbolic_macos/
作者
猫猫魔女
发布于
2025-10-02
许可协议
CC BY-NC-SA 4.0