How to Fix: Too Many Levels of Symbolic Links Error

The error “Too many levels of symbolic links” typically indicates a problem where a symbolic link points back to itself directly or indirectly, creating an infinite loop. This loop prevents the completion of operations involving the symlink such as reading or listing contents. Understanding how to properly use the ln command in Linux to create symbolic links is crucial for preventing this error.

In this tutorial you will learn:

  • What causes the “Too many levels of symbolic links” error
  • How to properly create symbolic links using absolute and relative paths
  • How to diagnose and fix loops in symbolic links
How to Fix: Too Many Levels of Symbolic Links Error
How to Fix: Too Many Levels of Symbolic Links Error
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any modern Linux distribution (Ubuntu, Fedora, etc.)
Software Bash shell
Other NA
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Understanding and Preventing “Too many levels of symbolic links” Error

What Causes This Error?

This error occurs when a symbolic link refers back to itself (either directly or through another symlink), which creates an unresolvable reference loop. This situation is analogous to being given a series of directions that loop back to the starting point, leading nowhere.

Example of Problematic Symlink Creation:

  1. Create a directory and change to it:
    $ mkdir /tmp/symlink_test
    $ cd /tmp/symlink_test
  2. Create a file and a directory:
    $ echo "This is a test file." > testfile
    $ mkdir testdir
  3. Create a symbolic link in testdir pointing to itself (erroneous):
    $ ln -s testfile testdir/testfile

    This command attempts to create a symlink testfile in testdir that points to ../testdir/testfile. The reference points back to the symlink itself, forming a loop.



  4. Trying to access the symlink results in an error:
    $ cat testdir/testfile
    Example of Problematic Symlink Creation causing How to Fix: Too Many Levels of Symbolic Links Error
    Example of Problematic Symlink Creation causing How to Fix: Too Many Levels of Symbolic Links Error

    This will output: cat: testdir/config: Too many levels of symbolic links

Correctly Creating Symlinks:

To avoid this error, you should ensure the target of the symlink correctly points to the intended destination and not back to the symlink itself or another symlink that leads to it.

Using Absolute Paths:

  1. Create a symbolic link using an absolute path:
    $ ln -s /tmp/symlink_test/testfile /tmp/symlink_test/testdir/testfile2

    This creates a symlink testfile2 in testdir that absolutely points to /tmp/symlink_test/testfile.

  2. Accessing this symlink should now correctly display the contents of testfile2:
    $ cat /tmp/symlink_test/testdir/testfile2

    This will correctly output: This is a test file.

    Correctly Creating Symlinks with absolute path
    Correctly Creating Symlinks with absolute path

Using Relative Paths:

  1. Alternatively, use a relative path wisely:
    $ ln -s ../testfile /tmp/symlink_test/testdir/testfile3

    This points testfile3 in testdir to testfile using a relative path that navigates up two levels and then to testfile.

  2. Checking the symlink:
    $ cat testdir/testfile3

    This will correctly output: This is a test file.

    Correctly Creating Symlinks with relative path
    Correctly Creating Symlinks with relative path

Symbolic Link Creation Simplified: Remembering the Correct Path Syntax – Author’s notes

My best strategy to deal with symbolic links and to remember to use them correctly—whether using relative or absolute paths—is to remember this syntax:

$ ln -s <path to original file from the target directory whether relative or absolute> <target file or directory> 

Let’s consider this example from within the /home/linuxconfig/ directory:

$ mkdir test
$ echo "linuxconfig.org" > orig_file
$ ln -s ../orig_file test/
$ ln -s ../orig_file test/sym_file1
$ ln -s /home/linuxconfig/../../tmp/../home/linuxconfig/orig_file test/sym_file2
$ ln -s ../../../home/linuxconfig/orig_file test/sym_file3
$ tree test/
Symbolic Link Creation Simplified: Remembering the Correct Path Syntax
Symbolic Link Creation Simplified: Remembering the Correct Path Syntax

As you can see from the above example, you can even combine relative and absolute paths, and still, the symbolic link works as long as it points correctly to the original file. This is not an easy concept to grasp, but think of the first argument as a path to the original file from the target directory. If you are in the target directory, the path supplied as the first argument to the ln command must be correct from there. To gain better understanding about relative and absolute paths consider to see section Relative vs Absolute Path as part of our bash scripting tutorial.

Conclusion

Correctly managing symbolic links is essential for maintaining a functional file system structure. By understanding how to properly set the targets of symlinks, either through absolute or relative paths, you can prevent errors related to infinite resolution loops, such as “Too many levels of symbolic links.” Always ensure your symlinks point to the correct targets and revise any existing links if you encounter this error.

 



Comments and Discussions
Linux Forum