4

If you need to recursively traverse a directory tree, there are two ways to do it:

  1. Build up pathnames of increasing length as you go, .../.../... etc.

  2. Use chdir to step down into each directory as you come to it, so you are never dealing with pathnames longer than two components.

The first method strikes me as more obvious, and might be more robust against untoward events like something being unmounted while you are halfway through it. On the other hand, looking over the code for the GNU find utility, I notice it uses the second method. Is there a reason for that? Any advantage of the second method that I hadn't thought of?

rwallace
  • 31,405
  • 40
  • 123
  • 242

3 Answers3

3

Erm... in fact a modern implementation will likely use the

ftw is short for file tree walk

See also a very useful resource: http://rosettacode.org/wiki/Walk_a_directory/Recursively#Library:_POSIX

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Interesting! How portable is that in practice? – rwallace Sep 11 '11 at 11:25
  • 1
    Depends on your practice; it is a BSD originated API. It is ubiquitous on linux; Solars [has it](http://book.chinaunix.net/special/ebook/addisonWesley/APUE2/0201433079/ch04lev1sec21.html#ch04ex08); AIX [has it in libc.a](http://publib.boulder.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.basetechref%2Fdoc%2Fbasetrf1%2Fnftw.htm)... I think it is pretty widespread. I don't know about potential differences – sehe Sep 11 '11 at 11:45
  • And http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/ftw.3.html says Mac OS has it; but claims new code should use fts instead? – rwallace Sep 11 '11 at 12:06
2

I believe the find uses method 2 as you are able to execute commands as you go (with the exec option)

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
2

The method 2 seemlessly handles situations where a component in the path is renamed.

It also denies anyone from unmounting the directory while it's being searched; the kernel will refuse to unmount the directory if it's in use, which includes being the cwd of some process.

Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194