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
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:
- Create a directory and change to it:
$ mkdir /tmp/symlink_test $ cd /tmp/symlink_test
- Create a file and a directory:
$ echo "This is a test file." > testfile $ mkdir testdir
- Create a symbolic link in
testdir
pointing to itself (erroneous):$ ln -s testfile testdir/testfile
This command attempts to create a symlink
testfile
intestdir
that points to../testdir/testfile
. The reference points back to the symlink itself, forming a loop. - Trying to access the symlink results in an error:
$ cat testdir/testfile
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:
- Create a symbolic link using an absolute path:
$ ln -s /tmp/symlink_test/testfile /tmp/symlink_test/testdir/testfile2
This creates a symlink
testfile2
intestdir
that absolutely points to/tmp/symlink_test/testfile
. - 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.
Using Relative Paths:
- Alternatively, use a relative path wisely:
$ ln -s ../testfile /tmp/symlink_test/testdir/testfile3
This points
testfile3
intestdir
totestfile
using a relative path that navigates up two levels and then totestfile
. - Checking the symlink:
$ cat testdir/testfile3
This will correctly output: This is a test file.
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/
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.