Why tree command mark symlink as recursive?

2

I have the following directories tree:

.
└── aaa
    └── bbb
        └── ccc

And create symlink:

ln -s ~/test/aaa/bbb slink

After this i run tree command:

tree -l

and see

.
├── aaa
│   └── bbb
│       └── ccc
└── slink -> /home/kaa/test/aaa/bbb  [recursive, not followed]

Why slink marked as recursive? Is it a bug or am I misunderstanding something?

KaHa

Posted 2018-04-13T12:58:00.167

Reputation: 23

Answers

2

You used the -l option:

Follows symbolic links if they point to directories, as if they were directories. Symbolic links that will result in recursion are avoided when detected.

Now, the reason it prints this warning is not that it would actually recurse. We can clearly see that it would simply list the bbb/ccc tree again.

My guess is that the real problem is the halting problem: the tree command cannot know whether it will or will not recurse infinitely, hence it has to stop already when it detects a symlink to a directory that it has already visited—even if following that directory would just print a subtree and not recurse.

Even if you could halt early or after a predefined number of recursions, this is all done in order to not consume infinite amounts of memory. At least that is my interpretation.

The respective code is here, by the way. If the inode number of the directory pointed to by the symlink is found in the hash table (the one that is populated with saveino and findino), it will simply skip it.

slhck

Posted 2018-04-13T12:58:00.167

Reputation: 182 472

Yes. Looks like you are right even if this extremely counterintuitive. To confirm this i have create another tree:

yyy/xxx/zzz

plus symlink "alink" and now:

alink -> /home/kaa/test/yyy/xxx

slink -> /home/kaa/test/aaa/bbb [recursive, not followed] – KaHa – 2018-04-13T14:42:52.110