(Unix) Directory traversal and symbolic links
September 7, 2023
If and when you set out to traverse through a Unix directory
hierarchy, whether to inventory it or to find something, you have
a decision to make. I can put this decision in technical terms,
about whether you use stat()
or lstat()
when identifying
subdirectories in your current directory, or put it non-technically,
about whether or not you follow symbolic links that happen to point
to directories. As you might guess, there are two possible answers
here and neither is unambiguously wrong (or right). Which answer
programs choose depends on their objectives and their assumptions
about their environment.
The safer decision is to not follow symbolic links that point to
directories, which is to say to use lstat()
to find out what is
and isn’t a directory. In practice, a Unix directory hierarchy
without symbolic links is a finite (although possibly large) tree
without loops, so traversing