Apparently unlink inside a readdir loop is supposed to work, although there is some bug in some versions of linux that prevent it from seeing more than a few hundred entries. Still, it shouldn't segfault. (gdb) print (DIR *) *cdirp $4 = (struct __dirstream *) 0x0 Shouldn't that be "print *(DIR *) cdirp"?