NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |ATTRIBUTES |STANDARDS |HISTORY |NOTES |EXAMPLES |SEE ALSO |COLOPHON | |
scandir(3) Library Functions Manualscandir(3)scandir, scandirat, alphasort, versionsort - scan a directory for matching entries
Standard C library (libc,-lc)
#include <dirent.h>int scandir(const char *restrictdirp,struct dirent ***restrictnamelist,typeof(int (const struct dirent *)) *filter,typeof(int (const struct dirent **, const struct dirent **))*compar);int alphasort(const struct dirent **a, const struct dirent **b);int versionsort(const struct dirent **a, const struct dirent **b);#include <fcntl.h>/* Definition of AT_* constants */#include <dirent.h>int scandirat(intdirfd, const char *restrictdirp,struct dirent ***restrictnamelist,typeof(int (const struct dirent *)) *filter,typeof(int (const struct dirent **, const struct dirent **))*compar); Feature Test Macro Requirements for glibc (seefeature_test_macros(7)):scandir(),alphasort(): /* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200809L || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCEversionsort(): _GNU_SOURCEscandirat(): _GNU_SOURCE
Thescandir() function scans the directorydirp, callingfilter() on each directory entry. Entries for whichfilter() returns nonzero are stored in strings allocated viamalloc(3), sorted usingqsort(3) with the comparison functioncompar(), and collected in arraynamelist which is allocated viamalloc(3). Iffilter is NULL, all entries are selected. Thealphasort() andversionsort() functions can be used as the comparison functioncompar(). The former sorts directory entries usingstrcoll(3), the latter usingstrverscmp(3) on the strings(*a)->d_name and(*b)->d_name.scandirat() Thescandirat() function operates in exactly the same way asscandir(), except for the differences described here. Ifdirp is relative, then it is interpreted relative to the directory referred to by the file descriptordirfd (rather than relative to the current working directory of the calling process, as is done byscandir() for a relative pathname). Ifdirp is relative anddirfd is the special valueAT_FDCWD, thendirp is interpreted relative to the current working directory of the calling process (likescandir()). Ifdirp is absolute, thendirfd is ignored. Seeopenat(2) for an explanation of the need forscandirat().
Thescandir() function returns the number of directory entries selected. On error, -1 is returned, witherrno set to indicate the error. Thealphasort() andversionsort() functions return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
EBADF(scandirat())dirp is relative butdirfd is neitherAT_FDCWDnor a valid file descriptor.ENOENTThe path indirp does not exist.ENOMEMInsufficient memory to complete the operation.ENOTDIR The path indirp is not a directory.ENOTDIR (scandirat())dirp is relative anddirfd is a file descriptor referring to a file other than a directory.
For an explanation of the terms used in this section, seeattributes(7). ┌───────────────────────────────┬───────────────┬────────────────┐ │Interface│Attribute│Value│ ├───────────────────────────────┼───────────────┼────────────────┤ │scandir(),scandirat() │ Thread safety │ MT-Safe │ ├───────────────────────────────┼───────────────┼────────────────┤ │alphasort(),versionsort() │ Thread safety │ MT-Safe locale │ └───────────────────────────────┴───────────────┴────────────────┘
alphasort()scandir() POSIX.1-2008.versionsort()scandirat() GNU.
alphasort()scandir() 4.3BSD, POSIX.1-2008.versionsort() glibc 2.1.scandirat() glibc 2.15.
Since glibc 2.1,alphasort() callsstrcoll(3); earlier it usedstrcmp(3). Before glibc 2.10, the two arguments ofalphasort() andversionsort() were typed asconst void *. Whenalphasort() was standardized in POSIX.1-2008, the argument type was specified as the type-safeconst struct dirent **, and glibc 2.10 changed the definition ofalphasort() (and the nonstandardversionsort()) to match the standard.
The program below prints a list of the files in the current directory in reverse order.Program source #define _DEFAULT_SOURCE #include <dirent.h> #include <stdio.h> #include <stdlib.h> int main(void) { struct dirent **namelist; int n; n = scandir(".", &namelist, NULL, alphasort); if (n == -1) { perror("scandir"); exit(EXIT_FAILURE); } while (n--) { printf("%s\n", namelist[n]->d_name); free(namelist[n]); } free(namelist); exit(EXIT_SUCCESS); }closedir(3),fnmatch(3),opendir(3),readdir(3),rewinddir(3),seekdir(3),strcmp(3),strcoll(3),strverscmp(3),telldir(3)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17scandir(3)Pages that refer to this page:open(2), closedir(3), dirfd(3), fnmatch(3), opendir(3), qsort(3), readdir(3), rewinddir(3), seekdir(3), telldir(3)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |