Linux / Command / mkdir / Command to create directories

In Linux, mkdir is the command to create a new directory. To create a multi-level path at once, use -p, which allows you to keep existing directories as they are while creating any missing parent directories.

Creating and checking directories

mkdir is short for make directory. The following command creates a directory with the name projects in the current directory if it does not exist and checks the result. Run it in an empty practice directory to avoid confusion with existing data.

pwd
mkdir projects
ls -ld projects

First, check the current working location with pwd, and then check whether the new directory information is displayed in ls -ld. mkdir usually does not display any message if successful. If a file or directory with the same name already exists, the default command shows an error.

Relative paths are based on the current location, and absolute paths starting like /, such as /home/user/projects, are specified from the root directory. Check the actual path and access permissions according to your environment.

Create along with parent directories

The following command creates the necessary parent directories if they do not exist. For example, if work does not exist in the practice location, it will create work, work/reports, and work/reports/2024 in order.

mkdir -p work/reports/2024
ls -ld work work/reports work/reports/2024

If you run mkdir work/reports/2024 without -p, it will fail when the parent path does not exist. Adding -p will not cause an error just because the path already exists as a directory. It does not delete existing contents or change existing directory permissions.

However, if there is a regular file with the same name in the middle of the path or if you do not have the necessary permissions for the parent directory, it may fail even if you use -p. Check the error message, exit status, and the actual path together to confirm success.

Multiple directories and names with spaces

You can list names separated by spaces to create multiple directories. The following is an example when the two names do not yet exist in the current location.

mkdir documents images
ls -ld documents images

If the name itself contains spaces, enclose the entire path in quotes to pass it as a single name. If there are no quotes, the shell will split it at the spaces and pass them as separate arguments.

mkdir "project files"
ls -ld "project files"

Names starting with a hyphen can be interpreted as options. For example, mkdir -- -archive places the name after --, so it is not treated as an option.

Check permissions and results when creating

Generally, the default permissions of a new directory are affected by the current shell's umask and the file system settings. Check the current mask with umask, and verify the actual permissions of the created directory with ls -ld.

You can also set the permissions of a newly created directory using the -m option of GNU mkdir. The following is an example where read, write, and execute permissions are granted to the owner and no permissions are given to the group and others when private does not yet exist.

mkdir -m 700 private
ls -ld private

Even if -p and -m are used together, -m is applied to the new directory specified on the command line and is not applied to automatically created parent directories. It also does not apply new permissions to directories that already exist. Access control lists or file system policies may affect actual access, so check the results in the necessary environment. For detailed behavior of the options, refer to the GNU mkdir manual.

When creation fails

  • Already exists: Use ls -ld name to check whether the existing item is a file or a directory. -p is appropriate only if you want to use the existing directory as is.
  • Parent path not found: Check the spelling of the path and your current location, then decide whether you can create the missing parent directories using -p.
  • Permission denied: Check whether you have the necessary write and access permissions for the parent directory and request them from the administrator if needed.

You can remove an empty directory created during practice with rmdir after confirming that it contains no content. rmdir does not generally remove directories that are not empty.