mkdir Command – Create New Directory in Linux

Last updated: June 11, 2023 | Linuxopsys

A directory is a location to store files and subdirectories. It is a file system object found in Linux, MS-DOS, OS/2, and Unix operating systems. The directory is also called folder is a friendly name more familiar to Windows users.

Mkdir (make directory) is the command to create a new directory in Linux. It also allows you to create multiple directories at once me.

Syntax

mkdir [option] directory-name

The following table shows mkdir options.

OptionsDescription
-p, --parentsCreate parent directories along with the target directory in the specified path.
-v, --verboseDisplay a confirmation message when the directory is created.
-m, --modeSet permission modes for the new directory. if not available use chmod command.
--versionDisplay the version number and exit.

Create a new Directory in Linux

To create a new directory in Linux type mkdir followed by the directory name:

mkdir myprojects

If the directory already exists mkdir gives an error. The mkdir never overwrites a directory.

mkdir create directory

This creates the directory named "myprojects" in the current directory. Type ls command to view the directory created:

view the directory created

After creating the directory you can use the cd command to change the directory.

cd myprojects 

Instead of creating in the current directory, you can give absolute path as well. For example to create a new directory named "myprojects" in the directory /home/ubuntu/, type:

$ mkdir /home/ubuntu/myprojects
create a new directory using absolute path

Here we need to make sure all parent directories exist or else it gives an error.

The ls -ld command can give additional information about the directory such as permission and last modification time. For timestamp information, you can use stat command.

To manipulate directories you can use mv to rename directories and cp to copying. You can use rmdir command to delete the directory. Only if the directory is empty and has write permission in its parent directory.

Note: To create a directory, the user should have write permission on the parent directory where you want to create the new directory. Or else need to use sudo with mkdir.

Print message when creating a Directory

The mkdir command won't give any output on the terminal on success. You can use -v option to display a confirmation message for the created directory.

mkdir -v dirname1
mkdir -v command

The message "mkdir: created directory 'dirname1" indicates that the directory was successfully created.

Examples

Let's look into some useful examples of mkdir command

Create Multiple Directories

To mkdir command we can pass multiple arguments separated by spaces to create multiple directories.

Syntax

mkdir dir1 dir2 dir3 ...

The following command creates 3 directories named dir1, dir2 and dir3

mkdir dir1 dir2 dir3

You may use curly braces {} to create multiple directories to specify a range or a list of values.

For example to create directories with a list of values:

mkdir {dir1,dir2,dir3}

You can see the dir1,dir2, and dir3 directories created.

Create directories using a numeric range

mkdir January{1..31}

By combining the values

mkdir {Jan,Feb,Mar}-{1..3}
mkdir using {} to create multiple directories with combination of values

Create Nested Directory Structure

You can mkdir -p command to create directories with the specified directory structure. This option make sure all the parent directories including the target directory in the path are created.

The mkdir command gives an error if the directory exists but with -p it doesn't. This is usually helpful when you are not sure the parent directory already exists.

For example, you have a directory named myprojects, the following mkdir -p command creates project01 in project1 inside the myprojects directory.

mkdir -p /myprojects/project1/project01
mkdir -p command

Specify Permissions

You can use mkdir -m command to give a specific mode or permission to the new directory. This can be only set while creating it. Once the directory is created the only option to change mode is to use chmod command.

The following command mkdir command creates a new directory named dir7 with 777 mode

mkdir -m 777 dir7
mkdir set mode for new directory

Some additional notes:

The default permission depends on umask value set on your system. The directory permission will be umask value subtracted from the default permissions (777). Most Linux distributions have an umask value 002 for the root and 002 for the regular user. When a regular user creates a new directory 755 will be default permission. Whereas as for root user it will be 775.

mkdir default directory permission
SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment