getdents(2)

๐Ÿ’– getdents โ€” get directory entries ๐Ÿ’–

okay bestie, welcome to getdents(2) โ€” the Linux man-pages 6.20 era edition, dated 2026-06-14, straight out of the Standard C library (libc, -lc). this one's a system call page, so buckle up ๐ŸŽ€


โœจ SYNOPSIS โœจ

#include <sys/syscall.h>      /* Definition of SYS_* constants */
#include <unistd.h>

long syscall(SYS_getdents, unsigned int fd, struct linux_dirent *dirp,
             unsigned int count);

#define _GNU_SOURCE           /* See feature_test_macros(7) */
#include <dirent.h>

ssize_t getdents64(size_t count;
                   int fd, void dirp[count], size_t count);
heads up: glibc provides no wrapper for getdents(), so you literally have to use syscall(2). no shortcut bestie ๐Ÿ˜ญ
also: there is no definition of struct linux_dirent in glibc. see NOTES. ๐Ÿ™ˆ

๐ŸŒธ DESCRIPTION ๐ŸŒธ

bestie. bestie listen. these are not the interfaces you are interested in. like, fr fr. go look at readdir(3) for the POSIX-conforming C library interface. that's the one you actually want. this one? this is the raw, unhinged, syscall-level situation. ๐Ÿ’…

๐Ÿฆ‹ getdents()

the system call getdents() reads several linux_dirent structures from the directory referred to by the open file descriptor fd, into the buffer pointed to by dirp. the argument count specifies the size of that buffer. periodt.

the linux_dirent structure is declared as follows:

struct linux_dirent {
    unsigned long  d_ino;     /* Inode number */
    unsigned long  d_off;     /* Not an offset; see below */
    unsigned short d_reclen;  /* Length of this linux_dirent */
    char           d_name[];  /* Filename (null-terminated) */
                      /* length is actually (d_reclen - 2 -
                         offsetof(struct linux_dirent, d_name)) */
    /*
    char           pad;       // Zero padding byte
    char           d_type;    // File type (only since Linux
                              // 2.6.4); offset is (d_reclen - 1)
    */
}

okay so let's break down these fields, because they're giving lore:

  • d_ino is an inode number. iconic.
  • d_off is a filesystem-specific value with no specific meaning to user space โ€” though on older filesystems it used to be the distance from the start of the directory to the start of the next linux_dirent. see readdir(3). it is not an offset, no cap.
  • d_reclen is the size of this entire linux_dirent.
  • d_name is a null-terminated filename.

d_type is a byte at the end of the structure that indicates the file type. it contains one of the following values (defined in <dirent.h>):

  • DT_BLK โ€” this is a block device.
  • DT_CHR โ€” this is a character device.
  • DT_DIR โ€” this is a directory.
  • DT_FIFO โ€” this is a named pipe (FIFO).
  • DT_LNK โ€” this is a symbolic link.
  • DT_REG โ€” this is a regular file.
  • DT_SOCK โ€” this is a UNIX domain socket.
  • DT_UNKNOWN โ€” the file type is unknown.

the d_type field is implemented since Linux 2.6.4. it occupies a space that was previously a zero-filled padding byte in the linux_dirent structure. so on kernels up to and including Linux 2.6.3, attempting to access this field always provides the value 0 (DT_UNKNOWN). ancient times, bestie ๐Ÿงš

currently, only some filesystems (among them: Btrfs, ext2, ext3, and ext4) have full support for returning the file type in d_type. all applications must properly handle a return of DT_UNKNOWN. no skipping this, it's mandatory ๐Ÿ’€

๐ŸŒˆ getdents64()

the original Linux getdents() system call did not handle large filesystems and large file offsets โ€” like, it just couldn't, it was going through it. consequently, Linux 2.4 added getdents64(), with wider types for the d_ino and d_off fields. plus, getdents64() supports an explicit d_type field. glow up โœจ

the getdents64() system call is like getdents(), except that its second argument is a pointer to a buffer containing structures of the following type:

struct linux_dirent64 {
    ino64_t        d_ino;    /* 64-bit inode number */
    loff_t         d_off;    /* Not an offset; see getdents() */
    unsigned short d_reclen; /* Size of this dirent */
    unsigned char  d_type;   /* File type */
    char           d_name[]; /* Filename (null-terminated) */
};

๐Ÿ’ซ RETURN VALUE ๐Ÿ’ซ

  • on success, the number of bytes read is returned. slay.
  • on end of directory, 0 is returned.
  • on error, -1 is returned, and errno is set to indicate the error. ๐Ÿ˜ญ

๐Ÿ‘‘ ERRORS ๐Ÿ‘‘

  • EBADF โ€” invalid file descriptor fd.
  • EFAULT โ€” argument points outside the calling process's address space.
  • EINVAL โ€” result buffer is too small.
  • ENOENT โ€” no such directory.
  • ENOTDIR โ€” file descriptor does not refer to a directory.

๐ŸŒธ STANDARDS ๐ŸŒธ

None. literally none. it's giving rogue behavior. ๐Ÿ’…


๐ŸŽ€ HISTORY ๐ŸŽ€

  • getdents() โ€” SVr4.
  • getdents64() โ€” glibc 2.30.

โœจ NOTES โœจ

glibc does not provide a wrapper for getdents(); call getdents() using syscall(2). in that case you will need to define the linux_dirent structure yourself. build it from scratch bestie ๐Ÿงต

the getdents64() system call accepts an unsigned int argument count and assumes that count does not exceed INT_MAX. the glibc wrapper function sets count to INT_MAX if it exceeds INT_MAX. also glibc defines a dirent64 structure, which is similar to linux_dirent64 but with a 256-byte d_name.

probably, you want to use readdir(3) instead of these system calls. like probably. ๐Ÿ™ˆ

these calls supersede readdir(2).


๐Ÿ”ฅ EXAMPLES ๐Ÿ”ฅ

the program below demonstrates the use of getdents(). the following output shows an example of what we see when running this program on an ext2 directory:

$ ./a.out /testfs/
--------------- nread=120 ---------------
inode#    file type  d_reclen  d_off   d_name
       2  directory    16         12  .
       2  directory    16         24  ..
      11  directory    24         44  lost+found
      12  regular      16         56  a
  228929  directory    16         68  sub
   16353  directory    16         80  sub2
  130817  directory    16       4096  sub3

๐Ÿฆ‹ Program source

#define _GNU_SOURCE
#include <dirent.h>     /* Defines DT_* constants */
#include <err.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

struct linux_dirent {
    unsigned long  d_ino;
    off_t          d_off;
    unsigned short d_reclen;
    char           d_name[];
};

#define BUF_SIZE 1024

int
main(int argc, char *argv[])
{
    int                  fd;
    char                 d_type;
    char                 buf[BUF_SIZE];
    long                 nread;
    struct linux_dirent  *d;

    fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
    if (fd == -1)
        err(EXIT_FAILURE, "open");

    for (;;) {
        nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
        if (nread == -1)
            err(EXIT_FAILURE, "getdents");

        if (nread == 0)
            break;

        printf("--------------- nread=%ld ---------------\n", nread);
        printf("inode#    file type  d_reclen  d_off   d_name\n");
        for (size_t bpos = 0; bpos < nread;) {
            d = (struct linux_dirent *) (buf + bpos);
            printf("%8lu  ", d->d_ino);
            d_type = *(buf + bpos + d->d_reclen - 1);
            printf("%-10s ", (d_type == DT_REG) ?  "regular" :
                             (d_type == DT_DIR) ?  "directory" :
                             (d_type == DT_FIFO) ? "FIFO" :
                             (d_type == DT_SOCK) ? "socket" :
                             (d_type == DT_LNK) ?  "symlink" :
                             (d_type == DT_BLK) ?  "block dev" :
                             (d_type == DT_CHR) ?  "char dev" : "???");
            printf("%4d %10jd  %s\n", d->d_reclen,
                   (intmax_t) d->d_off, d->d_name);
            bpos += d->d_reclen;
        }
    }

    exit(EXIT_SUCCESS);
}

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€


okay that's the whole page bestie, you ate that ๐Ÿ’– this was getdents(2), shipped in Linux man-pages 6.20 on 2026-06-14 โ€” go read readdir(3) fr fr, mwah ๐Ÿซถโœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms