如何停止递归符号链接

5
我有一个包含以下文件夹的文件夹:
  • 文件夹 A
    • 文件夹 B(符号链接到文件夹 A)
问题在于,当我访问文件夹B时,由于文件夹B位于文件夹A中,我可以无限地进入更深层级目录(即文件夹A > 文件夹B > 文件夹B > 文件夹B)。
是否有任何方法在通过文件夹A访问文件夹B后忽略文件夹B?
非常感谢!
2个回答

4
“find”命令可以检测符号链接循环,并打印警告消息,而不是重复遍历它们。例如,使用“-L”选项跟随符号链接可能会打印此警告:
$ find -L /some/path

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

来自 man 页面:

          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.

2
任何程序员看到这里(命令行工具问题应该去unix.stackexchange.com):
你应该知道Linux/BSD函数fts_open()为您提供了一个易于使用的迭代器,用于遍历所有子目录内容,同时还检测到这样的符号链接递归。
大多数命令行工具使用此功能来处理此情况。那些不使用此功能的工具通常会在符号链接递归方面遇到麻烦,因为手动处理这个问题很困难(任何人意识到这一点都应该使用上述函数)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接