Mkdir Command - Make or Create Directory in Linux + Options

Channel: Linux
Abstract: After creating the directory you can use the cd command to change the directory. cd myprojects You can use rmdir command to delete the directory. Only

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.

The mkdir command is used in Linux/Unix systems to create one or more new directories.

In this tutorial, we learn about mkdir command to create the directory in Linux.

Prerequisites
  • A Computer installed with Linux/Unix.
  • Basic knowledge to use command line.
  • An open mind to learn.
What is Mkdir command in Linux

Mkdir (make directory) is the command to create a new directory in Linux. You can also create multiple directories same time.

Syntax mkdir command:

mkdir [option] directory-name

Some of the main options of mkdir command:

OptionsDescription-p, --parentsCreate parent directories in the specified path.-v, --verboseDisplay message when the directory is created.-m, --modeSet file modes (like chmod). Set specific directory permissions.--versionDisplay the version number and exit. How to create or Make 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 exits mkdir gives an error. The mkdir never overwrites a directory.

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

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

cd myprojects 

You can use rmdir command to delete the directory. Only if the directory is empty and has write permission in its parent directory.

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 message for each created directory.

mkdir -v dirname1
Create Multiple Directories using mkdir

To create multiple directories using mkdir place directory names separated with comma between brackets {}.

For example,

mkdir {dir1,dir2,dir3}

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

Create Parent Directory using Mkdir -P

Earlier we mentioned mkdir give an error if the directory exists but mkdir -p doest give any error and also keep the directory unchanged.

Mkdir -p flag is usually used to create a directory structure with the parent directories. So you can easily create intermediate directories.

For example, you have a directory named myprojects, this creates project01 in project1 inside the myprojects directory.

mkdir -p /myprojects/project1/project01
Mkdir Permissions

The permission of the directory depends on umask value. The default directory permission is 777. The directory permission will be umask value subtracted from the default permissions values for directories.

Most Linux distributions have umask value 002 for root and 002 for the normal user.

So when a normal user creates a directory by permission will be 775 and when root creates it will be 755.

The mkdir command use -m option to give specific permission to the directory when creating it.

The following command creates a new directory named dir7 with 777 (read, write and execute) permissions:

mkdir -m 777 dir7
Conclusion

In this tutorial, we learned about mkdir command in Linux. Now you know how to create a directory using mkdir command.

Ref From: linuxopsys

Related articles