Mandrek
Mandrek

Reputation: 1211

How to create multiple directories using shell script

I am new to shell script, I am trying to create multiple directories using script at specific locations.i have no idea about that, i am giving here what i have tried so far ,

directory_name="/home/usw9bls7/lib1"

if [ -d $directory_name ]
then
echo "Directory already exists"
else
mkdir $directory_name
fi

I have to create directory at locations
"/home/usw9bls7/config1"
"/home/usw9bls7/DNQ/lib1"
"/home/usw9bls7/DNQ/config1"

Plesae help

Upvotes: 1

Views: 13646

Answers (4)

Petr Skocik
Petr Skocik

Reputation: 60058

Pass them all to mkdir -p:

mkdir -p \
"/home/usw9bls7/config1" \
"/home/usw9bls7/DNQ/lib1" \
"/home/usw9bls7/DNQ/config1" 

If you're super performance-conscious, you can test them for existence from the shell first. Here's what I'm doing in my shell lib:

all_eh()
{
    local predic a; predic=$1; shift
    for a; do
        $predic "$a" || return 1
    done
}
mkdir_p() { all_eh 'test -d' "$@" || mkdir -p "$@"; }

This is faster if all the directories exist because test (or [ ) is a shell builtin (in practically all shells) and so doesn't cost you the usual 1-2ms fork/exec overhead.

Upvotes: 0

Miguel Ortiz
Miguel Ortiz

Reputation: 1482

Don't use "if" statements for such simple task, use "-p, --parents" of mkdir to create several directories that doesn't exist and ignore existing ones.

Combine that capability using metacharacters to expand subfolders creations.

In this example I've created the next structure with one command:

-/home
   |---/usw9bls7
          |------- DNQ
                     |---- lib1
                     |---- config1

user@DESKTOP-RR909JI ~
$ mkdir -p /home/usw9bls7/DNQ/{lib1,config1}

user@DESKTOP-RR909JI ~
$ ls -ltr /home/usw9bls7/DNQ/
total 0
drwxr-xr-x+ 1 user user 0 jun. 29 20:17 lib1
drwxr-xr-x+ 1 user user 0 jun. 29 20:17 config1

None of these directories existed before in my environment (except for /home). Anyway if you want to create the script try simplifying things with this.

Upvotes: 3

Adam Katz
Adam Katz

Reputation: 16128

Portable POSIX solution (works in /bin/ksh, /bin/sh, and /bin/bash)

#!/bin/sh
parent_directory="/home/usw9bls7/lib1"

for directory_name in config1 DNQ/lib1 DNQ/config1
do
  if [ -d "$parent_directory/$directory_name" ]
  then
    echo "Directory already exists"
  else
    mkdir -p "$parent_directory/$directory_name"
  fi
done

This merely loops over your given names and then makes them. I added the -p option so that it will silently create missing parents like DNQ as needed.

Note that if these exist as files rather than directories, you'll get errors from mkdir. At least you'll get something.

Upvotes: 0

P....
P....

Reputation: 18371

Using brace expansion if you use bash:

directory_name="/home/usw9bls7/lib1"

if [ -d "$directory_name" ];then
   echo "Directory already exists"
else
    mkdir -p  /home/usw9bls7/{config1,DNQ/lib1,DNQ/config1}/"$directory_name"
fi

Example: with echo command.

echo /home/usw9bls7/{config1,DNQ/lib1,DNQ/config1}
/home/usw9bls7/config1 /home/usw9bls7/DNQ/lib1 /home/usw9bls7/DNQ/config1

Upvotes: 0

Related Questions