MKDIR(3P)
π MKDIR(3P) β bestie we're making DIRECTORIES π
okay so. this is mkdir(3P), straight out of the POSIX Programmer's Manual. the Linux version of this whole interface might do stuff differently (so like, go read the Linux man page for the tea), or honestly it might not even be implemented on Linux at all. π
PROLOG β this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
β¨ NAME β¨
mkdir, mkdirat β make a directory. that's it. that's the whole job. iconic behavior honestly π
πΈ SYNOPSIS πΈ
#include <sys/stat.h>
int mkdir(const char *path, mode_t mode);
#include <fcntl.h>
int mkdirat(int fd, const char *path, mode_t mode);
π¦ DESCRIPTION π¦
so mkdir() creates a brand new directory with the name path. no cap. the file permission bits of the new directory get initialized from mode, and then those permission bits get modified by the process' file creation mask. (yes, the umask is coming for your bits, that's just how it is.)
when bits in mode other than the file permission bits are set β like, the extra little guys β what those additional bits even mean is implementation-defined. nobody's telling you. it's a mystery. π
the directory's user ID shall be set to the process' effective user ID. the directory's group ID shall be set to the group ID of the parent directory or to the effective group ID of the process. implementations shall provide a way to initialize the directory's group ID to the group ID of the parent directory. implementations may, but do not have to, provide an implementation-defined way to initialize the directory's group ID to the effective group ID of the calling process. so like... it depends, bestie. read the room (and the implementation).
the newly created directory shall be an empty directory. blank slate. fresh start. main character energy. β
if path names a symbolic link, mkdir() shall fail and set errno to [EEXIST]. periodt.
upon successful completion, mkdir() shall mark for update the last data access, last data modification, and last file status change timestamps of the directory. also, the last data modification and last file status change timestamps of the directory that contains the new entry shall be marked for update. timestamps get touched, everyone gets updated, it's a whole thing. π₯
mkdirat() is literally the same as mkdir() except when path specifies a relative path. in that case, the newly created directory is created relative to the directory associated with the file descriptor fd instead of the current working directory. slay. if the access mode of the open file description associated with the file descriptor is not O_SEARCH, the function shall check whether directory searches are permitted using the current permissions of the directory underlying the file descriptor. if the access mode is O_SEARCH, the function shall not perform the check. no check, no stress. π
and if you pass mkdirat() the special value AT_FDCWD in the fd parameter, the current working directory shall be used and the behavior shall be identical to a call to mkdir(). same vibes, different function name. π«Ά
π RETURN VALUE π
upon successful completion, these functions shall return 0. otherwise, these functions shall return -1 and set errno to indicate the error. and if -1 is returned, no directory shall be created. none. zero. the directory simply did not happen. π
π ERRORS π
these functions shall fail if:
EACCESβ search permission is denied on a component of the path prefix, or write permission is denied on the parent directory of the directory to be created.EEXISTβ the named file exists. it's already there, bestie, move on. πELOOPβ a loop exists in symbolic links encountered during resolution of thepathargument.EMLINKβ the link count of the parent directory would exceed{LINK_MAX}.ENAMETOOLONGβ the length of a component of a pathname is longer than{NAME_MAX}.ENOENTβ a component of the path prefix specified bypathdoes not name an existing directory, orpathis an empty string. (empty string!! nothing!! nada!!)ENOSPCβ the file system does not contain enough space to hold the contents of the new directory or to extend the parent directory of the new directory. disk said "no room at the inn." πENOTDIRβ a component of the path prefix names an existing file that is neither a directory nor a symbolic link to a directory.EROFSβ the parent directory resides on a read-only file system.
in addition, the mkdirat() function shall fail if:
EACCESβ the access mode of the open file description associated withfdis notO_SEARCHand the permissions of the directory underlyingfddo not permit directory searches.EBADFβ thepathargument does not specify an absolute path and thefdargument is neitherAT_FDCWDnor a valid file descriptor open for reading or searching.ENOTDIRβ thepathargument is not an absolute path andfdis a file descriptor associated with a non-directory file.
these functions may fail if:
ELOOPβ more than{SYMLOOP_MAX}symbolic links were encountered during resolution of thepathargument.ENAMETOOLONGβ the length of a pathname exceeds{PATH_MAX}, or pathname resolution of a symbolic link produced an intermediate result with a length that exceeds{PATH_MAX}.
heads up: the following sections are informative. they're the gossip, not the law. π£
π EXAMPLES π
π Creating a Directory π
the following example shows how to create a directory named /home/cnd/mod1, with read/write/search permissions for owner and group, and with read/search permissions for others. look at those permission bits go:
#include <sys/types.h>
#include <sys/stat.h>
int status;
...
status = mkdir("/home/cnd/mod1", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
π§ APPLICATION USAGE π§
None.
(yeah. literally nothing. the void. we love a low-effort section. β¨)
π RATIONALE π
mkdir() originated in 4.2 BSD and was added to System V in Release 3.0. a true legend with lore.
4.3 BSD detects [ENAMETOOLONG]. good for them.
the POSIX.1β1990 standard required that the group ID of a newly created directory be set to the group ID of its parent directory or to the effective group ID of the creating process. FIPS 151β2 required that implementations provide a way to have the group ID be set to the group ID of the containing directory, but did not prohibit implementations also supporting a way to set the group ID to the effective group ID of the creating process. conforming applications should not assume which group ID will be used. if it matters, an application can use chown() to set the group ID after the directory is created, or determine under what conditions the implementation will set the desired group ID. so like... don't guess, bestie. verify. π
the whole point of mkdirat() is to create a directory in directories other than the current working directory without exposure to race conditions. any part of the path of a file could be changed in parallel to the call to mkdir(), resulting in unspecified behavior. by opening a file descriptor for the target directory and using the mkdirat() function, it can be guaranteed that the newly created directory is located relative to the desired directory. it's giving "we fixed the race condition by adding an at function." iconic. π₯
β FUTURE DIRECTIONS β
None.
(nothing on the horizon. we're at peace. ποΈ)
π SEE ALSO π
chmod(), mkdtemp(), mknod(), umask()
the Base Definitions volume of POSIX.1β2017: <fcntl.h>, <sys_stat.h>, <sys_types.h>
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie!! π«Ά this one was brought to you by IEEE/The Open Group, 2017 β go make some directories, you absolute icon. π β¨ππ¦πΈ
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- 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-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms