Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A program that recursively traverses a directory tree, needs to figure out what to do about symlinks (and/or junction points on Windows), to avoid going into an infinite loop when a symlink points to a parent directory. The two obvious options are:

  1. Skip symlinks completely.

  2. Go ahead and follow them, but keep track of all the directories visited, and skip whenever it's about to hit an infinite loop.

Which is the most common/expected/preferred choice?

share|improve this question
That depends what it's doing it for. If it's building a directory tree to enumerate files or build a visual representation, the second one, if it's totalling up filesizes, the first. But either way, make sure you tell me which way you're doing it. – Phoshi Sep 11 '11 at 11:38
Or if the API implements it, realize that it is a symlink . . . – surfasb Sep 11 '11 at 13:07

If you are just browsing, follow.

But, you specified that you don't want infinite loops...

to avoid going into an infinite loop when a symlink points to a parent directory

If you are searching recursively in a Breadth-First manner, follow. In a Depth-First manner, skip.

If you are counting up file sizes, skip.

Keeping track is often an optimization, rather than an implementation. Of course, you better have it.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.