Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

mkdir, mkdirat - make directory

&pagelevel(4)&pagelevel

Syntax

#include <sys/stat.h>

Optional
#include <sys/types.h> (End)

int mkdir(const char *path, mode_t mode);
int mkdirat(int fd, const char *path, mode_t mode); 

Description

mkdir() creates a new directory with the name path. The mode of the new directory is initialized from mode (see chmod() for values of mode). The file permission bits of the mode argument are modified by the file creation mask of the process (see umask()).

The directory's user ID is set to the process' effective user ID. The directory's group ID is set to the process' effective group ID, or if the S_ISGID bit is set in the parent directory, then the group ID of the directory is inherited from the parent. The S_ISGID bit of the new directory is inherited from the parent directory.

If path is a symbolic link, it is not used.

The newly created directory will be an empty directory, except for the entries for itself and its parent directory.

Upon successful completion, mkdir() will mark for update the st_atime, st_ctime and st_mtime fields of the directory. The st_ctime and st_mtime fields of the directory that contains the new entry are also marked for update.

The mkdirat() function is equivalent to the mkdir() function except when the path parameter specifies a relative path. In this case the new directory is not created in the current directory, but in the directory connected with the file descriptor fd. If the file descriptor was opened without O_SEARCH, the function checks whether a search is permitted in the connected file descriptor with the authorizations applicable for the directory. If the file descriptor was opened with O_SEARCH, the check is not performed.

When the value AT_FDCWD is transferred to the mkdirat() function for the fd parameter, the current directory is used.

Return val.

0

if successful.


-1

if an error occurs. No directory is created and errno is set to indicate the error.

Errors

mkdir() and mkdirat() will fail if: 

 

EACCES

Either there is no search permission for a component of the path prefix, or there is no write permission for the parent directory of the new directory.

 

EEXIST

The specified file already exists.

 

Extension

 

EFAULT

path points outside the allocated address space of the process.

 

EIO

An I/O error occurred when accessing the file system.

 

ELOOP

Too many symbolic links were encountered in resolving path. (End)

 

EMLINK

The maximum number of links {LINK_MAX} in the parent directory was exceeded.

 

ENAMETOOLONG 



The length of the path argument exceeds {PATH_MAX} or a component of path is longer than {NAME_MAX}.

 

ENOENT

A component of the path does not exist or path points to an empty string.

 

Extension

 

ENOLINK

path points to a remote computer and the link to that computer is no longer active. (End)

 

ENOSPC

No free space is available on the device containing the directory.

 

ENOTDIR

A component of the path is not a directory.

 

EROFS

The specified file resides on a read-only file system.

 

In addition, mkdirat() fails if the following applies: 

 

EACCES

The file descriptor fd was not opened with O_SEARCH, and the authorizations applicable for the directory do not permit the directory to be searched.

 

EBADF

The path parameter does not specify an absolute pathname, and the fd parameter does not have the value AT_FDCWD, nor does it contain a valid file descriptor opened for reading or searching.

 

ENOTDIR

The path parameter does not specify an absolute pathname, and the file descriptor fd is not connected with a directory.

Notes

mkdir() and mkdirat() are executed only for POSIX files. 

See also

chmod(), mknod(), umask(), stat(), fcntl.h, sys/stat.h, sys/types.h.