mkdir(2)
๐ mkdir(2) โ the System Calls Manual's glow-up edition ๐
okay bestie, welcome to mkdir(2) from the System Calls Manual โ which is lowkey the most iconic syscall for making a whole new folder out of thin air. new directory just dropped โจ
๐ธ NAME ๐ธ
mkdir, mkdirat โ create a directory ๐
๐ LIBRARY ๐
Standard C library (libc, -lc) โ she's standard, she's classic, she's in every libc ๐
โจ SYNOPSIS โจ
#include <sys/stat.h>
int mkdir(const char *path, mode_t mode);
#include <fcntl.h> /* Definition of AT_* constants */
#include <sys/stat.h>
int mkdirat(int dirfd, const char *path, mode_t mode);
๐ง Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
mkdirat():
- Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L
- Before glibc 2.10:
_ATFILE_SOURCE
๐ฆ DESCRIPTION ๐ฆ
so mkdir() attempts to create a directory named path. attempts!! she's trying her best bestie ๐ญ
The argument mode specifies the mode for the new directory (see inode(7)). It is modified by the process's umask in the usual way: in the absence of a default ACL, the mode of the created directory is (mode & ~umask & 0777). Whether other mode bits are honored for the created directory depends on the operating system. For Linux, see VERSIONS below. yes you literally have to scroll down, i don't make the rules ๐
The newly created directory will be owned by the effective user ID of the process. and the group?? okay so if the directory containing the file has the set-group-ID bit set, or if the filesystem is mounted with BSD group semantics (mount -o bsdgroups or, synonymously mount -o grpid), then the new directory will inherit the group ownership from its parent; otherwise it will be owned by the effective group ID of the process. familial vibes fr fr ๐ซถ
If the parent directory has the set-group-ID bit set, then so will the newly created directory. like mother like daughter, the bit gets passed down ๐
๐ mkdirat() ๐
mkdirat() operates in exactly the same way as mkdir(), except for the differences described here. she's her sister, not her clone ๐
If path is relative, then it is interpreted relative to the directory referred to by the file descriptor dirfd โ rather than relative to the current working directory of the calling process, as is done by mkdir() for a relative pathname.
If path is relative and dirfd is the special value AT_FDCWD, then path is interpreted relative to the current working directory of the calling process (like mkdir()). classic behavior, we love a fallback ๐
If path is absolute, then dirfd is ignored. sorry girl, you're not needed ๐
See openat(2) for an explanation of the need for mkdirat().
โญ RETURN VALUE โญ
mkdir() and mkdirat() return zero on success. On error, -1 is returned and errno is set to indicate the error. zero = slay, -1 = check errno, periodt ๐
๐ฅ ERRORS ๐ฅ
get comfy, bestie, there's a whole list. every single one matters, no skipping ๐
EACCESโ The parent directory does not allow write permission to the process, or one of the directories inpathdid not allow search permission. (See alsopath_resolution(7).)EBADFโ (mkdirat())pathis relative butdirfdis neitherAT_FDCWDnor a valid file descriptor. fake fd behavior, we don't know her ๐EDQUOTโ The user's quota of disk blocks or inodes on the filesystem has been exhausted.EEXISTโpathalready exists (not necessarily as a directory). This includes the case wherepathis a symbolic link, dangling or not. she's already here, go home ๐ซEFAULTโpathpoints outside your accessible address space.EINVALโ The final component ("basename") of the new directory's path is invalid (e.g., it contains characters not permitted by the underlying filesystem). the name is giving illegal characters ๐ญELOOPโ Too many symbolic links were encountered in resolvingpath. link loop loop loop ๐EMLINKโ The number of links to the parent directory would exceedLINK_MAX.ENAMETOOLONGโpathwas too long. girl sameENOENTโ A directory component inpathdoes not exist or is a dangling symbolic link. she's not there and never was ๐ENOMEMโ Insufficient kernel memory was available.ENOSPCโ The device containingpathhas no room for the new directory. the disk said "no room at the inn" ๐ENOSPCโ The new directory cannot be created because the user's disk quota is exhausted. same error, different reason, she's versatile like that ๐ENOTDIRโ A component used as a directory inpathis not, in fact, a directory. imposter syndrome but make it filesystem ๐ENOTDIRโ (mkdirat())pathis relative anddirfdis a file descriptor referring to a file other than a directory.EPERMโ The filesystem containingpathdoes not support the creation of directories. this fs said absolutely not ๐ซEROFSโpathrefers to a file on a read-only filesystem. ice cold, frozen, cannot write ๐งEOVERFLOWโ UID or GID mappings (seeuser_namespaces(7)) have not been configured.
okay so like โ heads up,ENOSPCandENOTDIReach show up twice in this list on purpose. same error code, totally different cause. don't come for me, come for the kernel ๐ซถ
๐ธ VERSIONS ๐ธ
Under Linux, apart from the permission bits, the S_ISVTX mode bit is also honored. the sticky bit is that girl โจ
๐ glibc notes ๐
On older kernels where mkdirat() is unavailable, the glibc wrapper function falls back to the use of mkdir(). When path is relative, glibc constructs a pathname based on the symbolic link in /proc/self/fd that corresponds to the dirfd argument. improvising, adapting, overcoming ๐
๐ STANDARDS ๐
POSIX.1-2024.
๐ง HISTORY ๐ง
mkdir()4.2BSD,SVr4,POSIX.1-1988.
mkdirat()- glibc 2.4, Linux 2.6.16,
POSIX.1-2008.
- glibc 2.4, Linux 2.6.16,
look at these vintage queens, ancient lineage, still thriving ๐
๐ NOTES ๐
There are many infelicities in the protocol underlying NFS. Some of these affect mkdir(). nfs stays messy, i fear ๐ญ
๐ซ SEE ALSO ๐ซ
mkdir(1), chmod(2), chown(2), mknod(2), mount(2), rmdir(2), stat(2), umask(2), unlink(2), acl(5), path_resolution(7)
and that's the whole page bestie!! go forth and make directories responsibly ๐โจ๐๐ฆ
this has been mkdir(2), brought to you by Linux man-pages 6.20, dated 2026-02-11 โ mwah, ily, drink water ๐
๐ธ
๐ฆ 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: GPL-1.0-or-later
- Copyright 1992, Drew Eckhardt <drew@cs.colorado.edu>
- Copyright 1993-1994, Ian Jackson <iwj10@cus.cam.ac.uk>
- Copyright 2006-2014, 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