That's an excellent follow-up question! It's important to understand when to use certain options and when to avoid them.
You would typically NOT use mkdir -p (or at least be very cautious about it) in situations where you:
-
Only want to create a directory if its parent explicitly exists: If your intention is that
mkdir new_dirshould only succeed if the current working directory is the correct parent, thenmkdir -pmight hide a typo or an incorrect path. For example, if you meant to create~/project/digital_garden/notesbut accidentally typed~/project/digitalgarden/notes,mkdir -pwould createdigitalgardenfor you, potentially leading to an incorrect directory structure. Without-p, it would give an error, alerting you to the mistake. -
Are concerned about creating unintended directories: Related to the point above, if you're working in a script or an automated process where an incorrect path could lead to unexpected new parent directories being created in the wrong place, omitting
-pensures thatmkdirwill only create the final specified directory, and only if all intermediate parents already exist. -
Need to know if the directory was actually newly created: If your logic depends on knowing whether
mkdirsuccessfully created a new directory versus finding an existing one,mkdir -pwon't tell you the difference easily.mkdirwithout-pwill typically return an error if the directory already exists, which can be useful for conditional logic. -
Are creating a single directory with no new parents: If you're simply creating a directory in the current location, like
mkdir my_new_folder, then-pis redundant. It won't hurt, but it's unnecessary overhead.
In summary, while -p is very convenient for creating deep directory structures, you might choose to omit it when precise control over parent directory existence is crucial, or when you want the command to explicitly fail if any parent path is incorrect or missing.
Does this help clarify situations where you might prefer not to use mkdir -p?