What happens if parent directories don't exist?

QuestionsQuestions8 SkillsProDec, 11 2025
079

That's a very important question directly related to the mkdir command and its -p option!

If you use the mkdir command without the -p (parents) option, and any of the parent directories in the specified path do not exist, the command will fail and return an error.

Let's illustrate with an example:

Suppose you want to create a directory /home/labex/project/digital_garden/notes/my_first_note, but the notes directory does not yet exist.

  1. Without -p:

    mkdir /home/labex/project/digital_garden/notes/my_first_note

    This command would likely produce an error message similar to:

    mkdir: cannot create directory ‘/home/labex/project/digital_garden/notes/my_first_note’: No such file or directory

    This happens because the system can't create my_first_note inside notes if notes itself isn't there. You would have to create notes first, then my_first_note.

  2. With -p:

    mkdir -p /home/labex/project/digital_garden/notes/my_first_note

    This command would successfully create notes (if it didn't exist) and then create my_first_note inside it, without any errors.

So, in short:

  • mkdir (without -p): Fails if parent directories don't exist.
  • mkdir -p: Automatically creates any necessary parent directories that don't exist.

For the current step, we're creating notes, projects, and resources inside digital_garden. Since you already created digital_garden in the previous step, these mkdir commands will work fine without -p. However, if you were creating a deeper structure (like digital_garden/notes/quick_thoughts) and notes didn't exist, you'd find mkdir -p very handy!

Does that make sense? Let me know if you have any more questions!

0 Comments

no data
Be the first to share your comment!