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 forgetdents(), so you literally have to usesyscall(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_inois an inode number. iconic.d_offis 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 nextlinux_dirent. seereaddir(3). it is not an offset, no cap.d_reclenis the size of this entirelinux_dirent.d_nameis 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,
0is returned. - on error,
-1is returned, anderrnois set to indicate the error. ๐ญ
๐ ERRORS ๐
EBADFโ invalid file descriptorfd.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
- Copyright 1995, Andries E. Brouwer <aeb@cwi.nl>
- Copyright 2008-2015, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms