What is the difference between mkdir and mkdir -p in Linux?

0141

The Difference Between mkdir and mkdir -p in Linux

In the Linux operating system, the mkdir command is used to create new directories (folders). However, there is a subtle but important difference between using the basic mkdir command and using the mkdir -p command.

mkdir Command

The basic mkdir command is used to create a single directory at a time. For example, if you want to create a directory called "documents" in the current working directory, you would use the following command:

mkdir documents

This command will create the "documents" directory in the current working directory. However, if the parent directory (the directory above the one you're trying to create) doesn't exist, the command will fail and return an error.

For example, if you try to create a directory called "documents/reports" and the "documents" directory doesn't exist, the command will fail:

mkdir documents/reports
mkdir: cannot create directory 'documents/reports': No such file or directory

mkdir -p Command

The mkdir -p command, on the other hand, is used to create a directory and any necessary parent directories that don't already exist. This is often referred to as creating a "nested" or "hierarchical" directory structure.

Using the same example as before, if you want to create a "documents/reports" directory and the "documents" directory doesn't exist, you can use the mkdir -p command to create both directories at once:

mkdir -p documents/reports

This command will create the "documents" directory if it doesn't exist, and then create the "reports" directory inside it.

The -p option stands for "parents", and it tells the mkdir command to create any necessary parent directories along the way.

graph TD A[Current Working Directory] --> B[documents] B --> C[reports]

In summary, the main difference between mkdir and mkdir -p is that mkdir -p can create a nested directory structure, even if the parent directories don't exist, while the basic mkdir command can only create a single directory at a time.

0 Comments

no data
Be the first to share your comment!