| 1 |
.\" Copyright 1992, Drew Eckhardt <[email protected]> |
| 2 |
.\" Copyright 1993-1994, Ian Jackson <[email protected]> |
| 3 |
.\" Copyright 2006-2014, Michael Kerrisk <[email protected]> |
| 4 |
.\" Copyright, the authors of the Linux man-pages project |
| 5 |
.\" |
| 6 |
.\" SPDX-License-Identifier: GPL-1.0-or-later |
| 7 |
.\" |
| 8 |
.TH mkdir 2 2026-02-11 "Linux man-pages 6.18" |
| 9 |
.SH NAME |
| 10 |
mkdir, mkdirat \- create a directory |
| 11 |
.SH LIBRARY |
| 12 |
Standard C library |
| 13 |
.RI ( libc ,\~ \-lc ) |
| 14 |
.SH SYNOPSIS |
| 15 |
.nf |
| 16 |
.B #include <sys/stat.h> |
| 17 |
.\" .B #include <unistd.h> |
| 18 |
.P |
| 19 |
.BI "int mkdir(const char *" path ", mode_t " mode ); |
| 20 |
.P |
| 21 |
.BR "#include <fcntl.h> " "/* Definition of AT_* constants */" |
| 22 |
.B #include <sys/stat.h> |
| 23 |
.P |
| 24 |
.BI "int mkdirat(int " dirfd ", const char *" path ", mode_t " mode ); |
| 25 |
.fi |
| 26 |
.P |
| 27 |
.RS -4 |
| 28 |
Feature Test Macro Requirements for glibc (see |
| 29 |
.BR feature_test_macros (7)): |
| 30 |
.RE |
| 31 |
.P |
| 32 |
.BR mkdirat (): |
| 33 |
.nf |
| 34 |
Since glibc 2.10: |
| 35 |
_POSIX_C_SOURCE >= 200809L |
| 36 |
Before glibc 2.10: |
| 37 |
_ATFILE_SOURCE |
| 38 |
.fi |
| 39 |
.SH DESCRIPTION |
| 40 |
.BR mkdir () |
| 41 |
attempts to create a directory named |
| 42 |
.IR path . |
| 43 |
.P |
| 44 |
The argument |
| 45 |
.I mode |
| 46 |
specifies the mode for the new directory (see |
| 47 |
.BR inode (7)). |
| 48 |
It is modified by the process's |
| 49 |
.I umask |
| 50 |
in the usual way: in the absence of a default ACL, the mode of the |
| 51 |
created directory is |
| 52 |
.RI ( mode " & \[ti]" umask " & 0777)." |
| 53 |
Whether other |
| 54 |
.I mode |
| 55 |
bits are honored for the created directory depends on the operating system. |
| 56 |
For Linux, see VERSIONS below. |
| 57 |
.P |
| 58 |
The newly created directory will be owned by the effective user ID of the |
| 59 |
process. |
| 60 |
If the directory containing the file has the set-group-ID |
| 61 |
bit set, or if the filesystem is mounted with BSD group semantics |
| 62 |
.RI ( "mount \-o bsdgroups" |
| 63 |
or, synonymously |
| 64 |
.IR "mount \-o grpid" ), |
| 65 |
the new directory will inherit the group ownership from its parent; |
| 66 |
otherwise it will be owned by the effective group ID of the process. |
| 67 |
.P |
| 68 |
If the parent directory has the set-group-ID bit set, then so will the |
| 69 |
newly created directory. |
| 70 |
.\" |
| 71 |
.\" |
| 72 |
.SS mkdirat() |
| 73 |
The |
| 74 |
.BR mkdirat () |
| 75 |
system call operates in exactly the same way as |
| 76 |
.BR mkdir (), |
| 77 |
except for the differences described here. |
| 78 |
.P |
| 79 |
If |
| 80 |
.I path |
| 81 |
is relative, then it is interpreted relative to the directory |
| 82 |
referred to by the file descriptor |
| 83 |
.I dirfd |
| 84 |
(rather than relative to the current working directory of |
| 85 |
the calling process, as is done by |
| 86 |
.BR mkdir () |
| 87 |
for a relative pathname). |
| 88 |
.P |
| 89 |
If |
| 90 |
.I path |
| 91 |
is relative and |
| 92 |
.I dirfd |
| 93 |
is the special value |
| 94 |
.BR AT_FDCWD , |
| 95 |
then |
| 96 |
.I path |
| 97 |
is interpreted relative to the current working |
| 98 |
directory of the calling process (like |
| 99 |
.BR mkdir ()). |
| 100 |
.P |
| 101 |
If |
| 102 |
.I path |
| 103 |
is absolute, then |
| 104 |
.I dirfd |
| 105 |
is ignored. |
| 106 |
.P |
| 107 |
See |
| 108 |
.BR openat (2) |
| 109 |
for an explanation of the need for |
| 110 |
.BR mkdirat (). |
| 111 |
.SH RETURN VALUE |
| 112 |
.BR mkdir () |
| 113 |
and |
| 114 |
.BR mkdirat () |
| 115 |
return zero on success. |
| 116 |
On error, \-1 is returned and |
| 117 |
.I errno |
| 118 |
is set to indicate the error. |
| 119 |
.SH ERRORS |
| 120 |
.TP |
| 121 |
.B EACCES |
| 122 |
The parent directory does not allow write permission to the process, |
| 123 |
or one of the directories in |
| 124 |
.I path |
| 125 |
did not allow search permission. |
| 126 |
(See also |
| 127 |
.BR path_resolution (7).) |
| 128 |
.TP |
| 129 |
.B EBADF |
| 130 |
.RB ( mkdirat ()) |
| 131 |
.I path |
| 132 |
is relative but |
| 133 |
.I dirfd |
| 134 |
is neither |
| 135 |
.B AT_FDCWD |
| 136 |
nor a valid file descriptor. |
| 137 |
.TP |
| 138 |
.B EDQUOT |
| 139 |
The user's quota of disk blocks or inodes on the filesystem has been |
| 140 |
exhausted. |
| 141 |
.TP |
| 142 |
.B EEXIST |
| 143 |
.I path |
| 144 |
already exists (not necessarily as a directory). |
| 145 |
This includes the case where |
| 146 |
.I path |
| 147 |
is a symbolic link, dangling or not. |
| 148 |
.TP |
| 149 |
.B EFAULT |
| 150 |
.I path |
| 151 |
points outside your accessible address space. |
| 152 |
.TP |
| 153 |
.B EINVAL |
| 154 |
The final component ("basename") of the new directory's |
| 155 |
.I path |
| 156 |
is invalid |
| 157 |
(e.g., it contains characters not permitted by the underlying filesystem). |
| 158 |
.TP |
| 159 |
.B ELOOP |
| 160 |
Too many symbolic links were encountered in resolving |
| 161 |
.IR path . |
| 162 |
.TP |
| 163 |
.B EMLINK |
| 164 |
The number of links to the parent directory would exceed |
| 165 |
.BR LINK_MAX . |
| 166 |
.TP |
| 167 |
.B ENAMETOOLONG |
| 168 |
.I path |
| 169 |
was too long. |
| 170 |
.TP |
| 171 |
.B ENOENT |
| 172 |
A directory component in |
| 173 |
.I path |
| 174 |
does not exist or is a dangling symbolic link. |
| 175 |
.TP |
| 176 |
.B ENOMEM |
| 177 |
Insufficient kernel memory was available. |
| 178 |
.TP |
| 179 |
.B ENOSPC |
| 180 |
The device containing |
| 181 |
.I path |
| 182 |
has no room for the new directory. |
| 183 |
.TP |
| 184 |
.B ENOSPC |
| 185 |
The new directory cannot be created because the user's disk quota is |
| 186 |
exhausted. |
| 187 |
.TP |
| 188 |
.B ENOTDIR |
| 189 |
A component used as a directory in |
| 190 |
.I path |
| 191 |
is not, in fact, a directory. |
| 192 |
.TP |
| 193 |
.B ENOTDIR |
| 194 |
.RB ( mkdirat ()) |
| 195 |
.I path |
| 196 |
is relative and |
| 197 |
.I dirfd |
| 198 |
is a file descriptor referring to a file other than a directory. |
| 199 |
.TP |
| 200 |
.B EPERM |
| 201 |
The filesystem containing |
| 202 |
.I path |
| 203 |
does not support the creation of directories. |
| 204 |
.TP |
| 205 |
.B EROFS |
| 206 |
.I path |
| 207 |
refers to a file on a read-only filesystem. |
| 208 |
.TP |
| 209 |
.B EOVERFLOW |
| 210 |
UID or GID mappings (see |
| 211 |
.BR user_namespaces (7)) |
| 212 |
have not been configured. |
| 213 |
.SH VERSIONS |
| 214 |
Under Linux, apart from the permission bits, the |
| 215 |
.B S_ISVTX |
| 216 |
.I mode |
| 217 |
bit is also honored. |
| 218 |
.SS glibc notes |
| 219 |
On older kernels where |
| 220 |
.BR mkdirat () |
| 221 |
is unavailable, the glibc wrapper function falls back to the use of |
| 222 |
.BR mkdir (). |
| 223 |
When |
| 224 |
.I path |
| 225 |
is relative, |
| 226 |
glibc constructs a pathname based on the symbolic link in |
| 227 |
.I /proc/self/fd |
| 228 |
that corresponds to the |
| 229 |
.I dirfd |
| 230 |
argument. |
| 231 |
.SH STANDARDS |
| 232 |
POSIX.1-2024. |
| 233 |
.SH HISTORY |
| 234 |
.TP |
| 235 |
.BR mkdir () |
| 236 |
4.2BSD, |
| 237 |
SVr4, |
| 238 |
POSIX.1-1988. |
| 239 |
.\" SVr4 documents additional EIO, EMULTIHOP |
| 240 |
.TP |
| 241 |
.BR mkdirat () |
| 242 |
glibc 2.4, |
| 243 |
Linux 2.6.16, |
| 244 |
POSIX.1-2008. |
| 245 |
.SH NOTES |
| 246 |
There are many infelicities in the protocol underlying NFS. |
| 247 |
Some of these affect |
| 248 |
.BR mkdir (). |
| 249 |
.SH SEE ALSO |
| 250 |
.BR mkdir (1), |
| 251 |
.BR chmod (2), |
| 252 |
.BR chown (2), |
| 253 |
.BR mknod (2), |
| 254 |
.BR mount (2), |
| 255 |
.BR rmdir (2), |
| 256 |
.BR stat (2), |
| 257 |
.BR umask (2), |
| 258 |
.BR unlink (2), |
| 259 |
.BR acl (5), |
| 260 |
.BR path_resolution (7) |