How to stop recursive symbolic link
Asked Answered
M

2

5

I have a the following folder :

  • Folder A
  • Folder B (which is a symbolic link to Folder A)

The issue is that when I access Folder B, I can go infinitely deeper (ie Folder A > Folder B > Folder B > Folder B) because Folder B is inside Folder A.

Is there any way to ignore Folder B after accessing it via Folder A ?

Minnesota answered 8/7, 2019 at 19:20 Comment(0)
A
5

The find command detects symbolic link loops and will print a warning message instead of traversing them repeatedly. For example, using the -L option to follow symlinks may print this warning:

$ find -L /some/path

find: File system loop detected; `/some/path/link' is part of the same file system loop as `/some/path'.

From the man page:

          The find utility shall detect infinite loops; that is,
          entering a previously visited directory that is an ancestor of
          the last file encountered.  When it detects an infinite loop,
          find shall write a diagnostic message to standard error and
          shall either recover its position in the hierarchy or
          terminate.
Aphrodisiac answered 8/7, 2019 at 19:27 Comment(0)
M
2

To any programmers looking here (cmdline tool questions should instead go to unix.stackexchange.com):

You should know that the Linux/BSD function fts_open() gives you an easy-to-use iterator for traversing all sub directory contents while also detecting such symlink recursions.

Most command line tools use this function to handle this case for them. Those that don't often have trouble with symlink recursions because doing this "by hand" is difficult (any anyone being aware of it should just use the above function instead).

Monotheism answered 28/7, 2021 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.