Bash script to create directory if not exists

Veeresh Kumar
2 min readOct 23, 2021

We often need to create some standard directory structure for our application or any other purpose. Sometimes we might have to do it in automated ways during runtime.

I have developed a script which takes directory names or directory full path’s in comma separated values and checks for its presence, If not creates it.

Here we go!!

Usage:

createDirsIfnotExists.sh -d <comma (,) separated directory names>

Now, lets create the bash script and create some directories:

  • Copy and Paste the below script and Save as createDirsIfnotExists.sh file.
  • Navigate to created script file path, and make the file executable by running this command.

sudo chmod u+x createDirsIfnotExists.sh

  • Now run the script by passing the directory paths as input like below example:

sudo bash createDirsIfnotExists.sh -d ‘/app/frontend,/app/backend,test’

  • Once the script finishes you can see the below output
  • Verify Directories created:

Since, we provided last directory path as test, so ‘test’ directory created in the script executing path or Present working directory folder.

  • If you re-run script with same inputs, it will not create those directories.

Hope it helps !!! Happy Scripting !!! Thank You !!!

--

--

Veeresh Kumar

He is a Sr. DevOps Engineer loves to automate cloud infrastructure and App deployments. #Terraform #Azure #Jenkins #Ansible #Chef #Bash #PowerShell #AWS #GCP