What does `-p` flag do?

QuestionsQuestions8 SkillsProDec, 18 2025
0251

Great question! The -p flag stands for "parents", and it is one of the most useful options for the mkdir command.

It primarily does two things:

1. Creates Parent Directories as Needed

Normally, if you try to create a folder inside a directory that doesn't exist yet, Linux will give you an error.

For example, if you try to run:

mkdir folder_a/folder_b

If folder_a doesn't exist, the command will fail.

However, if you use the -p flag:

mkdir -p folder_a/folder_b

Linux will automatically create folder_a first, and then create folder_b inside it. It creates the entire path for you!

2. Prevents "File Exists" Errors

By default, if you try to create a directory that already exists, mkdir will show an error:
mkdir: cannot create directory 'notes': File exists

If you use mkdir -p, it will not show an error if the directory already exists. It simply does nothing and moves on. This is very helpful when writing scripts where you want to ensure a folder exists without stopping the script if it's already there.

Summary:

  • Without -p: Can only create one level at a time and errors if the folder exists.
  • With -p: Creates the whole path (parents) and stays silent if the folder already exists.

Does that clarify how you might use it in your projects?

0 Comments

no data
Be the first to share your comment!