How to Use the mkdir Command in Linux

The mkdir Command creates new directories in Linux, enabling efficient folder organization. It supports multiple options, such as recursive parent directory creation and permission settings, crucial for Linux file system management.

🤖AI Overview:

The mkdir Command in Linux is a fundamental tool for creating one or more directories via the command line. It allows specifying permissions and supports automatic creation of parent directories, making it vital for managing file structures especially in headless or server environments.

The mkdir command [Make Directory Command] in Linux

By using the command line and the file manager, Linux create a folder, so to create single or multiple folders and directories in Linux, use the command and script called mkdir. Linux’s mkdir command, which stands for make directory, is a helpful tool for this;

Using this command, you can define a new directory, and if there is already a directory with the same name in the same path you specified, it will return an error that such a directory already exists. The “mkdir” command also allows you to create multiple directories with a single command, set permissions, and perform many other operations.

Prerequisites for running the mkdir command

  • Linux or Unix operating system
  • Access to the command line terminal (to execute the command)
  • Having sufficient user permission to create new directory linux

Create Directory in Linux using mkdir Command

The syntax required to execute the mkdir command is as follows:

mkdir [option] dir_name

Any number of directory names can be passed as arguments to this command.

Choosing Alternatives to the mkdir Command for Making a Directory

Option / SyntaxDescription
mkdir dir_nameThis command will create a new directory inside the current one.
mkdir {dir1,dir2,dir3,dir4}You can use this command to create more subdirectories in the current directory. Be careful you don't use spaces between the symbols{}.
mkdir –p dir/path/new_dirThis command will enable you to establish a directory structure that includes the missing parent directories (if there are any)
mkdir –m777 dir_nameUsing this command, you can provide all users complete read, write, and execute access to a new directory.
mkdir –v dir_name(s)With the information provided by this command, you can successfully execute the mkdir command in the current directory.

Create Directory in Linux using mkdir Command in 5 step

To execute the mkdir command, if access is defined for admin, go to the terminal and ensure the necessary access and proper permissions to perform the mkdir command.

Note: To execute the commands correctly, pay attention to upper and lower case letters; In case of typing wrongly and without attention to the capitalization and smallness of the letters, the meaning of the commands may change completely.

1. Directory name transfer

To create a new directory in Linux, pass the directory’s name as an argument to the mkdir command. For example, if you want to Linux create new directory called Newdir, you need to run the following command:

mkdir newdir

2. Confirm directory

If you enter this command, a new directory will be created in your current directory. After successfully creating the directory, you should see a blank line in the terminal. The “ls” command can be used to inspect and verify a directory after it has been created.

ls –l

If you do not specify the full path along with the directory name, the directory is created in the current working directory. The directory from which you run the commands is known as the “current working directory.” If you want to change the current working directory, you can use thecdcommand.

3. Creating a directory in another location

When creating a new directory elsewhere, you must specify an absolute or relative file path to the parent directory. For example, if you want to create a new directory in the /tmp folder, enter the following command:

mkdir /tmp/newdir

4. Create a directory in the parent directory

If you try to create a directory in the parent directory where the user does not have the required permissions, you will get a permission denied error.

mkdir /root/newdir

output

mkdir: cannot create directory '/root/newdir': Permission denied

5. Using the -v option (–verbose) to check the directory

Sometimes after running the “mkdir” command, you get no indication that the directory was created or failed. The “-v” option of the “mkdir” command displays detailed information about the directory creation process. The -v (–verbose) option instructs Mkdir to send a message for each newly created directory.

Note: After getting the information from the directory creation, you can skip the “ls” command to check the generated directory because you already have all the information you need about its creation.

How to Create Parent Directories

The “-p:” option of the “mkdir” command allows you to directory creation in Linux with multiple and different subdirectories. By selecting this option, you can ensure that the missing parent directory is created automatically.

To create a subdirectory inside another directory in Linux, for example, here, if you want to add “dirtest2” to “dirtest1”, you must use the “mkdir” command with the full path, as in the following procedure:

mkdir –p Linux/dirtest1/dirtest2

After running the “mkdir” command with the full path, you can use the “ls” command with the “-R” option to verify that the directory was created.

ls-R

If you forget the “-p” option, you will get an error from the terminal saying that the specified directory does not exist.

IF:

mkdir Linux/dirtest3/dirtest2

Output

mkdir: cannot create directory ' Linux/dirtest3/dirtest2' : No such file or directory

As you can see, not only was the new parent directory not created, but you got an error about dirtest3 not existing.

You will also get the same error if the parent directories do not exist.

If the parent directories do not exist, you can save time by using themkdircommand with the -p option instead of creating them manually.

Note: The -p option can only be used to create a new directory if it does not already exist.

To create a directory that already exists and the –p option is not provided, you will get a File exists error:

mkdir newdir

output

mkdir: cannot create directory 'newdir': File exists

Create Directory in Linux using mkdir Command

Permission settings for directory creation

When creating any directory on a Linux system, the “rwx” permission is granted exclusively for the benefit of the user who created the directory. If you want to have specific permission for the created directory using the “mkdir” command, the “-m” or (-mode) option changes the directory permissions for all users.

For better understanding, let’s define an example where we set the permission as “777” for the new directory, which means that by setting this permission, the user who created the directory will have access to the file, and the user can also read, write and execute the file.

mkdir -m {permissions} {dirName}

 

For example:  

mkdir -m 777 newdir

Next:

ls –l

If you decide to check the new directory’s specifications and permissions, you should be able to access an extensive list of directories by typing “ls -l” at the command line.

Note: If you do not specify the -m option when creating a new directory, newly created directories corresponding to the unmask value will be specified with permissions of 775 or 755 by default.

Creating multiple directories

The “mkdir” command can be used to separately create multiple directories within a given directory, While you have to spend more time executing the commands individually. In other words, the mkdir command can also be used to launch a complex directory structure with a single command. Therefore, if you are looking for a solution that does not waste a lot of time running separate commands and can quickly create multiple directories, using the “mkdir” command is the best option. In the “mkdir” command, you can separate the names of different directories by commas and add a list of directories to a directory.

For example, look at this for creating multiple folders at once.

mkdir {test1,test2,test3}

Note: You don’t need to leave space inside {} between directory names; By putting a distance, You will need a new name.

Consider the following example:

mkdir -p Art/{ Cubism / Fauvism, Impressionism , Realism , Symbolism /{ Expressionism, Constructivism , Abstract }, Romanesque /Baroque/ Rococo }

The tree directory you see below is the result of the above command:

Art/
|-- Romanesque
| `-- Baroque
| `-- Rococo
|-- Realism
|-- Impressionism
|-- Cubism
| `-- Fauvism
`-- Symbolism
|-- Expressionism
|-- Abstract
`-- Constructivism

Conclusion

The mkdir command is a fundamental and versatile tool for directory management in Linux environments. Understanding its syntax, options, and practical use cases equips developers to maintain organized, secure, and efficient file systems. Leveraging features like recursive directory creation, permission settings, and verbose output facilitates effective automation and system administration. 

By integrating the mkdir command adeptly into workflows, developers enhance productivity and ensure consistent directory management across projects and environments.

We encourage you to apply these best practices with the mkdir command in your development and administrative tasks. Should you have any further questions or require clarification, feel free to engage in discussion forums or consult official documentation for advanced usage scenarios.

FAQ

Use the -p option. For example "mkdir -p /path/to/new/directory" creates all parent directories if missing.

The -m option sets permissions during creation, e.g., "mkdir -m 777 directory_name" sets full permissions.

mkdir returns an error unless used with -p, which suppresses the error and does not duplicate directories.

List them separated by spaces: "mkdir dir1 dir2 dir3" creates multiple directories in one command.

Use ls or ls -l to list directories or "mkdir -v" for verbose confirmation.

You need write permissions in the target location; otherwise, mkdir returns a permission denied error.

A directory name without a path creates it in the current directory; absolute or relative paths specify location explicitly.

Yes, using -p and brace expansions, e.g., "mkdir -p parent/{child1,child2/subchild}".

Check user permissions, use sudo if necessary, and verify the target directory path.

Leave a Reply

Your email address will not be published. Required fields are marked.


Helen Publish in May 24, 2023 at 6:23 am

is mkdir the only way for linux folder create?

    Liosa F Publish in May 26, 2023 at 11:21 pm

    yes

Thomas Publish in May 24, 2023 at 6:09 am

What is the easiest way to Linux how to create a directory?

    Liosa F Publish in May 26, 2023 at 11:20 pm

    The only and easiest way is mkdir command.

William Publish in May 24, 2023 at 5:58 am

How do we set permissions when Linux make directory?

    Liosa F Publish in May 26, 2023 at 11:19 pm

    Invoking the mkdir command with the -m (-mode) option allows you to create a directory with particular permissions.