logo
Published on

Ln

Authors
  • avatar
    Name
    Bowen Y
    Twitter

ln command

The ln command links the file designated in the SourceFile parameter to the file designated by the TargetFile parameter or to the same file name in another directory specified by the TargetDirectory parameter. By default, the ln command creates hard links. To use the ln command to create symbolic links, designate the -s flag.

The issue happens when the folder name is the same because the relative path can unintentionally create a circular reference. When a symbolic link is created using a relative path and the folder has the same name, the system may resolve the symlink incorrectly, causing it to point back to itself. This leads to an infinite loop, which is why you get the "Too many levels of symbolic links" error.

The ln -s command relies on how the relative path is specified in relation to the symlink location, not the current directory from which you run the command.

How to Avoid This Problem:

Use Absolute Paths: One simple way to avoid this issue is to use absolute paths. Absolute paths don't depend on the current directory, so the system can easily locate the correct target without getting confused by relative paths:

ln -s /tmp/test2 static/test1

Use a correct Relative Path: ln test1 static/test2 this command will create a link test2 in the static folder, pointing to static/test1, not the test1 file in where you type the command. So use ln ../test1 static/test2 if you want it point to test1 file at your current folder.