How to Create a New Directory in Linux

You can use the “mkdir” command to create new directories in Linux. Creating a new directory in Linux is easy — just use the mkdir command:

$ mkdir New_Dir
$ ls -ld New_Dir
drwxrwxr-x. 2 geek geek 21 Mar  6 14:40 New_Dir
$

The system creates a new directory named New_Dir . Notice in the new directory’s long listing that the directory’s record begins with a d . This indicates that New_Dir is a directory.

Occasionally, you may need to create directories and subdirectories in “bulk.” To do this, add the -p option to the mkdir command as shown here:

$ mkdir -p New_Dir/SubDir/UnderDir
$ ls -R New_Dir
New_Dir:
SubDir
 
New_Dir/SubDir:
UnderDir
 
New_Dir/SubDir/UnderDir:
$

The -p option on the mkdir command makes any missing parent directories as needed. A parent directory is a directory that contains other directories at the next level down the directory tree.

mkdir Examples

1. Create directory with specified octal permissions.

# mkdir -m750 [dirname]

By default, your shell’s umask controls the permissions.

2. Set SELinux security context of each created directory to the default type.

# mkdir -Z

3. Given a directory path (not just a simple directory name), create any necessary parent directories automatically.

# mkdir -p /one/two/three

Creates the directories /one and /one/two if they don’t already exist, then /one/two/three.

You can learn more about the mkdir command and its options on its man pages. Type man mkdir and press return to view the command’s man pages.

# man mkdir
Related Post