Linux Tactic

Mastering mkdir: Create Directories Effortlessly in the Terminal

Getting familiar with different command line interfaces might seem daunting, but it doesn’t have to be. One of the most practical commands you should be aware of is the mkdir command.

This command enables you to create new directories (folders) right from your terminal. In this article, we will cover all there is to know about the mkdir command.

Absolute and Relative Paths

Before we dive into creating directories, it’s important to understand the difference between absolute and relative paths. An absolute path refers to the entire directory path that leads to the directory or file you want to work with.

This path generally starts with a forward slash (/) and includes all directories in between, separated by a forward slash. For example, /home/user/Documents.

On the other hand, a relative path is a path relative to your current working directory. In simpler terms, it is the distance between the directory you are currently in and the directory you want to work with.

Relative paths do not start with the forward slash. Instead, they start with a dot (.) or double-dot (..).

The dot refers to a directory in the current working directory, while two dots are used to navigate to the parent directory.

Creating a Single Directory with Mkdir

Now that you have an understanding of absolute and relative paths, let’s look at how to create a single directory with mkdir. Using the command is very simple.

You only need to type mkdir followed by the name of the directory you want to create. For example, to create a directory named “Project,” all you have to do is type:

“`

mkdir Project

“`

By running this command, you will create a new directory named “Project” in your current working directory. You can verify that it was created by listing the content of the directory using the command:

“`

ls

“`

This command will display all the directories and files in the current working directory, including our newly created “Project” directory.

Creating a Directory with a Specific Path

Sometimes you need to create directories in different paths. For instance, you may need to create a directory in your home directory or create a nested directory.

In such cases, you may need to specify the full directory path. To create a directory in a specified path, simply specify the full directory path along with the name of the directory you want to create.

For example, to create a directory named “Project” inside the Documents directory, type:

“`

mkdir /home/user/Documents/Project

“`

Alternatively, let’s say your current working directory is `/home/user/`, and you want to create a new directory named “Project” inside the Documents directory. You can use a relative path by typing:

“`

mkdir Documents/Project

“`

Note that you should only use relative paths if you know your current working directory.

Using an absolute path is more reliable if you are unsure where you are currently writing the command.

Conclusion

Now that you have learned the basics of the mkdir command, you can create new directories seamlessly from your terminal. Remember to use absolute paths when you want to specify a specific path and relative paths when creating directories in your current working directory.

As you continue to master the command line, you may discover more advanced features of the mkdir command in different shel

ls. However, this article provides a strong foundation for creating new directories easily.

So, make sure to practice the above steps to become more efficient and comfortable when using the mkdir command in the terminal.

3) Creating a Directory with Parent Directories Using the -p Flag

When working on a project, you may need to create a directory that has one or more subdirectories. However, you may find that you need to create a directory and its subdirectories simultaneously and you don’t want to create them separately.

In such a scenario, you will need to create a parent directory that houses all the subdirectories we intend to create.

A parent directory is simply a directory that contains other directories.

For example, if we want to create a directory called “project” with two subdirectories named “src” and “vendor,” we need the project directory to serve as the parent directory to the “src” and “vendor” directories. To create a parent directory along with its subdirectories in one command, we use the `-p` flag with the `mkdir` command.

The `-p` flag creates the necessary parent directories required to create the subdirectories if they don’t already exist. For example, suppose you want to create the following directory structure:

“`

/home/user/Downloads/Project/src

/home/user/Downloads/Project/vendor

“`

If the directories “Downloads” and “Project” do not exist in the path `/home/user/`, you can create the entire directory structure with the following command:

“`

mkdir -p /home/user/Downloads/Project/src /home/user/Downloads/Project/vendor

“`

The `-p` flag creates the “Downloads” directory and the “Project” directory along with the “src” and “vendor” subdirectories.

It’s important to note that the `-p` flag can create multiple parent directories at once, but we should be cautious not to create long or complicated directory structures, which can make navigating the directories challenging.

4) Creating Multiple Directories with Mkdir

Creating several directories simultaneously is crucial to save time and simplify directory creation. Creating multiple directories with `mkdir` is similarly easy as creating one directory, but with a little twist.

To create multiple directories simultaneously, use the `mkdir` command followed by the names of the directories you intend to create separated by spaces. For example, let’s say you want to create directories named “src,” “vendor,” “docs,” and “tests” in your current working directory, run the following command:

“`

mkdir src vendor docs tests

“`

This command will create all the directories named above in your current working directory. If you need to create directories with similar paths, such as “src/child1,” “src/child2,” and “src/child3,” you can use curly braces to create them simultaneously.

Simply enclose the directory names you want to create in curly braces {} and separate them with commas. The `mkdir` command will create all the directories with similar paths concurrently.

For example:

“`

mkdir src/{child1, child2, child3}

“`

This command will create the “src” directory and its child directories simultaneously: “src/child1,” “src/child2,” and “src/child3.”

In conclusion, the `mkdir` command is a powerful tool to create directories from the terminal. Understanding how to use the command can significantly increase productivity and simplify your work.

Using the `mkdir` command to create parent directories, multiple directories, and directories with similar paths can be a more straightforward process with the introduction of the `-p` flag and curly braces. These features not only save time but a

lso enable programmers to create and manage file structures efficiently.

5) Verbose Mode of Mkdir

By default, when you create a directory using the `mkdir` command, it does not display any output or error message. This default behavior is useful when creating a single directory, but when you need to create multiple directories or complex directory structures, it can be helpful to print messages to the console to provide more information on what is happening as the command runs.

The solution to this problem is to use the `-v` or `–verbose` flag to tell `mkdir` to display each directory being created. For example, if you needed to create a directory called “Project” and turn on verbose mode, you would type the following command:

“`

mkdir -v Project

“`

When you run this command, `mkdir` will print a message to the terminal for each created directory. It displays the name of the created directory as it is being made and whether it already existed or not.

This information can be helpful in tracking the progress of the command. Sometimes, you might create a directory and not remember the full path.

Using verbose mode with the `-p` flag gives a complete path of the parent directory when it is created. For instance, consider the following command:

“`

mkdir -vp /home/user/work/project/src

“`

In this example, the `-vp` option creates not only the directory `project/src`, but it a

lso displays a message listing all the parent directories created, including `work` and `project`.

Another useful application for the verbose mode is to spot when a directory already exists. For example, when you want to verify the existence of the project directory before creating it, you can type:

“`

mkdir -pv /home/user/work/project

“`

Assuming `project` already exists, verbose mode will inform you that “directory already exists” for the `project` folder.

Verbal mode may be useful, but it comes with some limitations. Running verbose mode with complex directory structures can be hard to read due to the numerous messages printed by the command.

It can a

lso interfere with an output stream, making some commands difficult to see. In conclusion, the verbose mode can be an incredibly useful feature of the `mkdir` command.

It’s a powerful tool that allows developers to maintain productivity, stay aware of the file system they are working with, and troubleshoot any issues that may arise during directory creation. However, it should be used judiciously to avoid any unnecessary clutter in the terminal and interference with other commands.

In conclusion, the `mkdir` command is a crucial tool for developers creating directories from the terminal. Understanding how to use the command can significantly increase productivity and simplify work.

We have learned how to create a single directory, create parent directories and subdirectories, create multiple directories simultaneously, and use the verbose mode with the `-p` or `-v` flag to display messages in the terminal. Incorporating the `mkdir` command into your workflow can help to streamline your file management processes and keep your work organized effectively.

Always remember to use the command judiciously to avoid any unnecessary clutter in the terminal and interference with other commands.

Popular Posts