How to Create a Directory in the Linux Terminal: The mkdir Command

mkdir command in linux
mkdir command in linux

This blog post will discuss the process of creating a directory in the Linux terminal using the mkdir command. The mkdir command creates directories in Linux. You will learn how to use the command to create directories and tips on making them correctly. Let's get started!

mkdir command syntax

The syntax for the mkdir command is as follows:

mkdir [option] directory_name

The options for the mkdir command are as follows:

  1. -p: This option will create any necessary parent directories if they do not exist. For example, if you want a directory called "foo" in the "/home/user" directory, but the "/home/user" directory does not exist, using the -p option will create both the "/home/user" and "foo" directories.

  2. -v: This option will print out a message for each created directory.

  3. -m: This option allows you to set the permissions for the new directory. By default, the mkdir command will create directories with "read," "write," and "execute" permissions for the owner and read and execute permissions for everyone else.

mkdir example:

To create a directory called "foo" in your current working directory, you would use the following command:

mkdir foo

This command would create a directory called "foo" in your current working directory with read, write, and execute permissions for the owner and read and execute permissions for everyone else. If you wanted to change the permissions of the "foo" directory so that only the owner could read, write, and execute it, you would use the following command:

mkdir -m 700 foo

This command would create a directory called "foo" in your current working directory with read, write, and execute permissions for the owner and no permissions for everyone else.

Stay tuned for Linux file permissions.

Nested directories

Making nested directories is also possible with the mkdir command. To make a directory called "foo" inside of another directory called "bar," you would use the following command:

mkdir -p bar/foo

This command would create a " foo " directory inside the "bar" directory. If the "bar" directory does not exist, the command will create it first.

Multiple directories

Making multiple directories at once is also possible with the mkdir command. To create three directories called "foo," "bar," and "baz" in your current working directory, you would use the following command:

mkdir foo bar baz

This command would create three directories called "foo," "bar," and "baz" in your current working directory. These directories would have read, write, and execute permissions for the owner and read and execute permissions for everyone else.

Now that we know how to use the mkdir command let's take a look at some tips on creating directories properly.

Tips on using mkdir command properly

  1. Use lowercase letters when creating directories. Linux is case-sensitive, meaning using uppercase letters in directory names can cause problems. For example, if a directory called "Foo" is available and you try to access it by using the "FOO" command, you will get an error message saying that the directory does not exist. To avoid this problem, always use lowercase letters when creating directories.
  2. Use numbers and underscores in directory names. Using spaces in directory names can cause problems because the Linux command line interprets them as arguments. For example, trying to access the directory called "My Documents" using the "My_Documents" command, brings an error message. To avoid this problem, always use numbers and underscores in place of spaces when creating directories.

Follow these tips to avoid problems when creating directories in the Linux terminal.

Conclusion

In this article, we have provided a simple guide on how to use the mkdir command, as well as some tips on best practices for creating directories. We hope this blog post has been informative and helpful. Leave your questions in the comments section.

Thanks for reading!

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.