Creating a directory in Bash seems trivial until your script runs twice and crashes on the second run with mkdir: cannot create directory 'backup': File exists. The pattern known as bash make directory if not exists solves this by making your scripts idempotent — safe to run once, twice, or a hundred times with the same result. This guide covers the fastest one-liner, the explicit conditional approach, reusable functions, permission handling, and the subtle mistakes that quietly break automation scripts in production.
Why "Create Only If Missing" Matters
Scripts are rarely run exactly once. They get re-run after a failure, scheduled by cron, executed by CI pipelines, or triggered by a deployment tool that retries on timeout. A naive `mkdir /var/log