1

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

4
  • @ImAtWar please check my edited post Commented Jun 29, 2017 at 14:31
  • 2
    use '-p' as option to mkdir to create missing intermediate directories. Commented Jun 29, 2017 at 14:54
  • Possible duplicate of How to create nonexistent subdirectories recursively using Bash? Commented Jun 29, 2017 at 20:43
  • Hi Mandrek, consider using metacharacters and --parents of mkdir to simplify things, I've provided you an example, hope it helps to your purpose. Commented Jun 29, 2017 at 23:25

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You didn't have a /home folder? Gasp
0

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

4 Comments

getting : command not found ./test.sh: line 8: syntax error near unexpected token fi' ./test.sh: line 8: fi'
Your line endings may be corrupted.
give output of echo $SHELL and try adding #!/bin/bash to first line of your script.
@PS now giving -bash: ./test.sh: /bin/ksh^M: bad interpreter: No such file or directory
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.