mkdir – Used to Create Directories

The mkdir command in Linux is used to create directories. It is a simple but essential command that allows users to create directories or folders in their file system.

Overview

The basic syntax of the mkdir command is as follows:

mkdir [OPTION]... DIRECTORY...

Here, DIRECTORY is the name of the directory that you want to create. You can create multiple directories at once by specifying their names separated by spaces.

For example, to create a directory named mydir in your current working directory, you can use the following command:

mkdir mydir

You can also create multiple directories at once by specifying their names like this:

mkdir dir1 dir2 dir3

By default, the mkdir command creates directories with read, write, and execute permissions for the user who created them. Other users may not have any permissions to access or modify the directory.

Options

The mkdir command has several options that you can use to modify its behavior. The available options are listed in the following table:

Option Description
-m, --mode=MODE Set the file mode (permissions) of the created directories to MODE.
-p, --parents Create parent directories as needed. If any parent directory does not exist, it will be created.
-v, --verbose Print a message for each directory that is created.
-Z, --context[=CTX] Set the SELinux security context of the created directories to CTX.

For example, to create a directory named mydir with read, write, and execute permissions for the owner and read and execute permissions for other users, you can use the following command:

mkdir -m 755 mydir

To create a directory named mydir and all its parent directories if they do not exist, you can use the following command:

mkdir -p /path/to/mydir

Troubleshooting Tips

If you encounter an error while using the mkdir command, it is likely that there is a problem with the directory name or the permissions. Here are some common issues and their solutions:

  • Permission denied: If you do not have permission to create a directory in a specific location, try creating it in a different location or use the sudo command to run the mkdir command with root privileges.
  • Directory already exists: If you try to create a directory that already exists, the mkdir command will return an error. Check if the directory already exists and choose a different name or location for the new directory.

Notes

  • The mkdir command creates directories with default permissions of rwxr-xr-x (755 in octal).
  • If you use the -p option to create a directory and its parent directories, the mkdir command will not return an error if the directory already exists.